asp.net mvc legacy route mapping - c#

I have a legacy asp.net website I am migrating to asp.net mvc.
I would like to permanently redirect existing urls to the asp.net mvc controllers. I have controllers with views for the new location for these links, and I would like to do a 301 redirect on the existing pages to the new views.
I have two different types of urls:
http://foosite.com/privacy.aspx
http://foosite/bar/content-name
Type 2 urls are the result of an existing url rewriter module from before asp.net mvc route handling.
I have existing redirect code:
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", url);
Response.End();
Where should I do the redirect?
I see two options:
Application_BeginRequest - use regex to parse the url
What I like about it:
I already check for uppercase urls to redirect to lowercase urls here
I have the chance to work directly with the response without having to return an ActionResult
What I don't like about it:
Url type #2 from the top has to be mapped into the new controllers/views, meaning I need to do some database work to get the path for the url
Controller Actions - use the routes & controllers to do the redirect
What I like about it:
I can cleanly do the database work I need
What I don't like about it:
The controller needs to return a view, and I am directly manipulating the Response stream to create the 301.
Any suggestions would be great, thanks.

Among those two choices, I would go with Controller actions. Controllers aren't required to return a view--I believe you can even make a controller method return void with ASP.NET MVC. The reason I like this option is because of the database interaction--I think spinning up a database in the BeginRequest is going to affect overall performance.
If that's not a concern, I think putting it with the rest of the routing information makes the most sense (i.e. with BeginRequest).

Related

Routing of vanity URLs within .NET Core without redirect

Within .NET Core 2, I would like to route a "vanity" URL like www.domain.com/harveyrelief to www.domain.com/donation/1326 which is serviced by the "donation" controller and "index" action within our website. The list of vanity URLs is maintained within our content management system with new ones being added weekly/monthly.
I could certainly pick these up from a wildcarded route and/or error controller and redirect to the donation controller/action. However, I want to maintain the original vanity URL entered by the donor.
With .NET Core's new architecture, pipeline, etc. is it possible to turn around and redirect processing of a URL within the pipeline and/or custom router without issuing a redirect to the browser?
It seems you are looking for the URL Rewriting Middleware: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-2.0&tabs=aspnetcore2x

How does ASP.NET MVC invoke different controller when URL never changes?

In this Adding a Controller (C#) tutorial I read:
ASP.NET MVC invokes different controller classes (and different action
methods within them) depending on the incoming URL
However, I am looking now at the source code full of such Controllers, the web app is fully functional, switches from Controller to Controller smoothly, without ever changing the URL...
How does this work?
Also, based on method, so one action might handle POST requests because it is decorated with [HttpPost] attribute and another one for GET requests as it is decorated with [HttpGet] attribute
I need to do some more guesswork as you've not detailed your observation: your application might send AJAX requests, and while the Url of the page is not changing, different actions in controllers are invoked because of these behind the scene requests.

ASP.NET MVC routing conflict

I have two routes in my application, each in a different Controller, that look like this:
[Route("forgot-password", Order = 1)]
[Route("{variable}", Order = 2)]
When I run the application I get the exception:
Multiple controller types were found that match the URL. This can
happen if attribute routes on multiple controllers match the requested
URL.
Remember these actions are in different Controllers. The Order attribute doesn't seem to work across Controllers!.
How can I get this scenario to work in asp.net mvc routing? I want to use attribute based routing and I don't want to change my urls.
The mechanism behind the error is explained here (I guess you are using Web API). Shortly put, precedence is honored only inside a controller.
Also, it is unclear for me why you want such a generic route like:
[Route("{variable}")]
The problem is that all routes in your application have been stored together. Even if it is located in different controllers, it is the same type, so they can "see" each other. In your case "forgot-password" and "{variable}" have the same format, that's why error about multiple routes has been displayed. As #NightOwl888 said, you may use RouteConfig to create a routes, but in that case you have to change your routing values.

How to allow some controllers to be visited not others in ASP.NET MVC 4

I will deploy my project in IIS and WS 2008.
I want to know how can I allow some controllers to be visited just inside my network and block people outside to see it.
Use the Request.Url property to check if the request is coming from another domain. Based on the url you can hide this.
Based on this
Either you check the url in controller action method or
Write Custom Authorization attribute

MVC RouteConfig vs URL rewrite (in Global.asax)

Myself and a colleague had a problem that we solved in two different ways. But we don't know which is best.
We have a generic MVC page that is populated with specific data (widgets, content, etc) from a database. The user enters a specific URL (user friendly, so trying to keep the query string disguised if we can help it).
Now, the generic page has to take this URL and use it to get the corresponding data from the database to generate a specific page.
Solution 1: In the Global.asax file rewrite the URL, basically creating a Querystring that the generic controller can understand.
Soloution 2: Use the RouteConfig.cs file to force all page requests to route to the generic controller, which then reads the URL.
Any ideas,
Thanks.
RouteConfig.cs is the cleanest way.
If you change your routing configuration, then by using Url.RouteUrl or Url.Action your generated Url's will update along with your routing configuration.

Categories

Resources