5

This question should be answered in somewhere, but I'll try to explain the scenario:

I have an application ASP.NET MVC, that works fine when I debug it locally OR deploying in Root IIS, but if I deploy it as an application on IIS (using alias), all referenced files will point to wrong place.

I'm using (for testing) my local machine as web server. By default, the application redirects to application/Login address.

Examples:

1) If I deploy the application in C:\inetpub\wwwroot, and access it via URL http://localhost, the website works fine, there is no file break or whatever. The website redirects the user to page http://localhost/Login

2) If I deploy the application in C:\inetpub\wwwroot\app1, and access it via URL http://localhost/app1, all referenced link and redirects are linking to http://localhost instead of http://localhost/app1, and when the default redirect occurs, it redirects me to http://localhost/Login, instead of http://localhost/app1/Login.

I tried to use the resource "location" on Web.config, setting the path to ".", "~" (that crashed the application) and "/", setting the inheritInChildApplications to true/false, setting this at one node above System.web node, but none of these attempts worked to me.

And it's no use setting the redirect to Login on RouteConfig, because it will solve only the Login redirect problem, but not the whole problem. Consider that the BundleConfig is reading in the same way.

I left the system.web node in Web.config node like this:

  <location path="." inheritInChildApplications="false">
<system.web>
  <authentication mode="Forms">
    <forms loginUrl="~/Login" timeout="60" />
  </authentication>
  <roleManager enabled="true">
  </roleManager>
  <compilation debug="true" targetFramework="4.5.2" />
  <httpRuntime targetFramework="4.5.2" />
</system.web>

Matheus Gelinski
  • 438
  • 1
  • 5
  • 15
  • 1
    When you deployed in the `app1` folder, did you also create an IIS application named `app1`? – John Wu Jul 05 '17 at 18:52
  • @JohnWu Yep. I'm creating the application and publishing it with the same name. – Matheus Gelinski Jul 05 '17 at 19:35
  • Did you try to browse the application from IIS? Just see what's the path it's deployed with when you launch the application from IIS – Amit Mishra Jul 06 '17 at 06:56
  • @AmitMishra this was my first attempt. When I clicked in "browse", it redirects me to localhost/app1 correctly, but when it redirects me to login page, the application redirects me according the situation described in the question. – Matheus Gelinski Jul 06 '17 at 12:02
  • Have you tried doing this? – Amit Mishra Jul 06 '17 at 13:09
  • Because your app is in C:\inetpub\wwwroot\app1 and what you've written in the loginUrl doesn't say the same. It will always search in C:\inetpub\wwwroot which is not the right path it seems. – Amit Mishra Jul 06 '17 at 13:17
  • @AmitMishra This solution you suggested me doesn't solve the whole problem. I said in own question: "And it's no use setting the redirect to Login on RouteConfig, because it will solve only the Login redirect problem, but not the whole problem. Consider that the BundleConfig is reading in the same way." – Matheus Gelinski Jul 06 '17 at 13:36

1 Answers1

0

Disclaimer: I have no idea how to do this from inside teh web.config You need to workout the hosting base address. See this Answer: https://stackoverflow.com/a/19866294

You could use VirtualPathRoot property from HttpRequestContext (request.GetRequestContext().VirtualPathRoot)

This will only get you the path under the server base address, so to get the full URL of the running IIS application I use:

var baseUrl = string.Format("{0}{1}/", Request.RequestUri.GetLeftPart(UriPartial.Authority), RequestContext.VirtualPathRoot);

Once you have this you can redirect inside the server side code

Morvael
  • 3,478
  • 3
  • 36
  • 53