ASP .Net MVC 5 redirect issue - c#

Currently I have an internal server serv that has an application app.
It is running MVC 5 and has 2 pages Account/Logon which maps to controller and an action.
And Orders/Display (again controller/action)
In order for internet users in the outside world to see I have a host server host running IIS. It maps host URLs to the internal app like so
host/Account/Logon -->serv/app/Account/Logon
So basically anything after host/... gets changed to serv/app/....
My problem is if I do RedirectToAction("Display","Orders");
(there are lots of these redirects because there's lots of controller/action combos.
The internal server grabs the host portion but also tacks on "app" in order
to switch to the next page or route
So,host/app/Orders/Display
My problem is that the host IIS maps this URL to host/app/app/Orders/Display
because that what it's mapping is set to do. always add "app"
How can I solve this issue? Is there any way for serv do
a relative mapping? In order words it only sees the original host/Account/Logon
so on switch it would be host/Orders/Display?

Use Redirect() instead of RedirectToAction() and pass in the url.

Related

How do I setup reverse proxy in local IIS

I have to implement reverse proxy setup in my local system. I have two website and in first website i have content and second website have empty forlder, I have one website example - www.nawaz-data.com and it has main content. and I have another website example - www.nawaz-proxy.com which is empty folder .
Requirement :
Need how do i setup for reverse proxy. When i request to www.nawaz-proxy.com than it should be redirect to www.nawaz-data.com where i have actual website content. I have tried to setup inbound and outbound rule but unable to get result it shows : HTTP Error 403.14 - Forbidden as this domain has no content, i want to redirect this url to another url where content has stored.
I have installed IIS windows 10
I have installed URL Rewrite
I have installed Application Request Routing Cache - in that I have enable proxy setting
According to your description, I suggest you could try to use below steps to add the reverse proxy rules.
1.Open the IIS mangement console, locate the right web site and click the url rewrite
2.Click the Add rule and select reverse proxy.
3.Add the servername op IP in below window, for example: www.nawaz-data.com. Notice: you should make sure your server could access the "www.nawaz-data.com" directly by using brower. If you couldn't access it ,you should use server's ip address directly.

How does Kestrel know the virtual directory it is running under in IIS?

We are trying to migrate our aspnetcore web apps from our own Windows/IIS server to AWS using ECS and API Gateway. We have everything working but ran into an unexpected issue with redirects inside our web applications. Our web site is setup like this in IIS:
/ => the root website is pointing to another web app
/app1 => app1 is setup as a virtual directory pointing to an empty folder
/app1/v1 => v1 is setup as an application pointing to an aspnetcore app
/app2/v1 => again v1 is setup as an application pointing to a different aspnetcore app
I have a very simple out of the box aspnetcore application that is setup as a virtual directory under /app1/v1. The routes in the aspnetcore application are:
/Web
/Web/Login
/Web/Home
When a user first visits the site (http://localhost/app1/v1/Web) they are redirected to the login page using:
Redirect("~/Web/Login")
When the application is run by IIS that results in the browser being redirected to:
http://localhost/app1/v1/Web/Login
However when I use a reverse proxy, like AWS API Gateway, that has been setup with the same virtual path it redirects to:
http://localhost/Web/Login
I'm guessing that there must be a configuration value that I need to pass into Kestrel during startup or as a request header to tell it to add "/app1/v1/" to any ~/ redirects that it performs?. Does anyone know how IIS is telling Kestrel the full path and how I can replicate that behavior?
Whenever asp.net runtime redirects,it will always use the base path
from the root .In your case the problem is ~ passed.This is telling
the browser to redirect from the base path which is /.
You can do remove the ~ and try giving Redirect("Web/Login").But I have seen asp.net runtime always add Reidrect Response wiht Location Header as "~/Web/Login".
So if removing the ~ does not work,you can try with
Response.RedirectToAbsoluteUrl("Web/Login");
Edit:
To explain the scenario why this may be happening when you do reverse proxy
Normally you setup a reverse proxy like this
www.example.com => servername:8080
When the proxy forwards the request,server(asp.net) does not know anything about the www.example.com ,to that asp.net runtime the request hostname is servername.
Now when you do redirect, what the asp.net runtime sends is a 302 response
This will look like this
Client request:
GET /app1/v1/Webl HTTP/1.1
Host: localhost
Server response:
HTTP/1.1 302 Found
Location: ~/Web/Login
Please check out this with a fiddler if you would like to confirm
Now when behind a reverse proxy where you may have setup like this
www.example.com/myapp => servername:8080
www.example.com/anotherapp => servername2:8080
You can double check how is the reverse proxy setup.Now imagine the same 302 response but your application does not know what is the relative path (www.example.com/myapp) .This causes problem with the what Location header is send in the response(if it has ~,this causes problems with relative paths in the original URL .
Hope this helps!.

Request to a IIS server (when ASP NET WebForms + MVC co-exist)

Well, there are many questions on SO which proves that you have the possibility to hold both ASP WebForms as well as ASP NET MVC Applications running together.
Note:
In MVC, What happens ? The short answer is Every requests to the webserver first goes through aURLRoutingModule object which reads the request and performs route-selection (depending upon global.asax file). The MvcRouteHandler then handles the controller method invocation.
That is okay.
All I am trying to understand is what happens when a user sends a request to the browser when the web server contains both Web-Forms as well as an ASP.NET MVC Application.
Who reads the URL and understands whether it is an .aspx extension or it is a request to mvc ?
How exactly the ISAPI.dll function in this case?
It's actually fairly simple: the URLRoutingModule checks the disk to see if there is a physical file which matches the current request URL. If there is a match, the Routing module assumes that it shouldn't run for this request, and ASP.NET's default behavior is then to look up and invoke the default handler for this extension. In the case of .aspx, that would be the Web Forms handler.
You can change this via the RouteCollection.RouteExistingFiles property in your RegisterRoutes method. This property controls whether the Routing module should intercept the request regardless of whether there is a physical file on the hard drive.
To understand better what happens when a user sends a request to the browser when the web server contains both Web-Forms as well as an ASP.NET MVC Application is by knowing on how ASP.Net runtime perform the Url routing which it is actually done by the ISAPI filter plugin.
Below are the detailed steps for Webform request:
1) When user enter a url like website1.com/page1.aspx in the browser.
2) Browser will send a Get Request to the IIS server asking for Page1.aspx file.
3) Then ISAPI filter is the first process to access the Http data before IIS can start to do anything. Here it act like a router for IIS request.
4) ISAPI filter will then check the url whether the filename has any extension (.aspx) and check whether the (.aspx) extension is mapped to the aspnet_isapi.dll or not.
5) If ".aspx" is mapped, it will invoke the ASP.net UrlRoutingModule and mapped the HttpHandler for ASP.net files.
For MVC request:
1) It follow the same step like webform request until the ISAPI filter found that there is no filename and file extension exist in the Url since MVC Url only contain the controller and action name.
2) All Url need to be mapped to a handler when the request initially received because routing will not work with unmanaged handler request
3) To direct this unmanaged request to managed code, it can be done by setting wildcard mapping to aspnet_isapi.dll for IIS6. For IIS7, we can just set the runAllManagedModulesForAllRequest="True".
4) Then only ASP.Net UrlRoutingModule will be invoked and try to match the Url with the application defined route.
5) If there is a match, it will direct to HttpHandler of Asp.net MVC framework. But if there is no match, the default Asp.net webform mapping will be used and most likely the request will return a HTTP 404 error result.
For more details you may refer to this web:
Mixing Web Forms and ASP.NET MVC
Web Server and ASP.NET Application Life Cycle in Depth

Loading routes according to HTTP_HOST in MVC4

I have a multi-tenant MVC4 website in which I want to set up the the RouteTable according incoming host and domain name eg www.domain1.com, www.domain2.com and www.domain3.co.uk could have one or more additional values appended to the route table according to the host and domain anem.
The DNS server has been set up with a number of distinct zones with hosts, and I'm using IIS7 as a 'custom web server' rather than using Visual Studio 2012 Development Server so that I get the correct HTTP_HOST value available to me in the Request variable.
The problem I have with IIS7 is that to the value of HttpContext.Current.Request.ServerVariables["HTTP_HOST"] is not available if you try to call it in the RegisterRoutes method where all the other route mapping is done.
The main thing I need to do is determine at some point in the request pipeline the the value that will eventually end up in the HTTP_HOST server variable . That value needs storing and the route table appropriately updated before it gets used.
Will it need an Http Module, Http Handler, Action Filter or somesuch? Or is there some other place in the MVC pipeline that I can do this.
Crispin
There's no built in support for routing based on host that I'm aware of, but you can create a Route subclass that pulls the host value in. Here's an example:
http://blog.maartenballiauw.be/post/2009/05/20/aspnet-mvc-domain-routing.aspx
That's written for MVC3, but should work fine in MVC4.

Running ASP.NET MVC application behind a proxy with different root relative path

I'm having trouble with paths in a ASP.NET MVC application that's running behind a proxy.
Our IIS Application root path is for example http://server/MyApp/
meaning that all urls using the application root ("~/",Url.Action("MyAction","MyController")) are resolved to "/MyApp"
Now we're running behind a proxy server that forwards all requests, but requires you to access the application through a URL like this:
"/Secury/Proxy/RubbishUrl/MyApp"
Because the proxy url is only available on the client, I thought of creating a cookie with the path prefix, and insert this before each generated URL on the server.
Now the question is, what's the best location in code to modify each URL that's resolved/sent to the client (to resources, controller actions, images etc)?
Every path in the application is resolved with the MVC methods (Url.Content, Url.Action etc).
Update:
Not actively looking for an answer anymore (though still interested in a proper solution)
Most of the time Proxies do their own URL translation, however in this case the proxy server is ignoring paths that are transfered in JSON, and they are processed.
My 'solution' for now is just not passing paths in JSON, but instead:
using proper ID's and values in the JSON requests
creating template in URL's in the HTML (which are resolved properly),
replace the ID's and values in the URL template with the values from the JSON requests
This method is actually a much 'cleaner' way IMO then passing the URL's.
You can create your own asp.net mvc controller factory where to decide which controller and action will serve the response based on the requested url. Check this url for a good blog post on how to do this - http://nayyeri.net/custom-controller-factory-in-asp-net-mvc.

Categories

Resources