ASP.NET MVC routing conflict - c#

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.

Related

ASP.NET WebApi specify action

I need to create ASP.NET WebApi with couple operation, for example RegisterAppStart, RegisterStep and RegisterAppEnd. And I want to place all this actions in one controller. As I read, usually in WebApi action name is not using.
And my question - is this a bad idea to rewrite default route config with actions using?
ps. sorry for my English
You can give actions arbitrary names using the [Route("/api/FooController/BarMethod"] attribute routing.
This usually overrides the "REST"yness of your service (where the request method indicates which method should be called), but as you aren't really building a REST service, you shouldn't care.

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.

Registering routes on session start not application start

I am trying to register the route collection in .net based on each session. The code I have works fine, you goto website/username and it loads the correct sub pages, but you have to restart the application to goto website/username2 to load those sub pages. But when the application is restarted the second one works, but then not the first one.
Is there some way to create a different route collection per session not per application using system.web.routing.
Routes are linked to route names and are stored globally for the web application, so you can't really define routes per session.
Can you give an example of why you need different routes for different users?
Most likely it can be solved by simply using route patterns, like setting RouteUrl to somehting like : "member/logo/{size}/{UserName}.jpg", where you can specify UserName and size when generating the route url via Page.GetRouteUrl()
You can create your own route handler. Take a look at : ASP.Net MVC Framework - Create your own IRouteHandler.
Using such approach, you will be able to route differently per request. Each request can then take a look at your session values to get the correct handler.
RouteTable.Routes is static so the same routes are shared across all Sessions. So you can't have a different set of routes for each session.

asp.net mvc legacy route mapping

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).

MVC C# custom MvcRouteHandler - How to?

Does anyone have experiences in providing a custom MvcRouteHandler? In my application I'd like to implement a globalization-pattern like http://mydomain/en/about or http://mydomain/de/about.
As for persistance, I'd like to have a cookie read as soon as a request arrives and if there is a language setting in this cookie apply it (so a user arriving at http://mydomain/ would be transferred to http://mydomain/en/ for example). If there is no cookie present, I'd like to get the first language the browser supports, apply this one and store it in this cookie.
I guess this can't be done with the standard routing mechanism mvc provides in it's initial project template. In a newsgroup I got the tip to have a look at the MvcRouteHandler and implement my own. But its hard to find a sample on how to do that.
Any ideas?
I don't believe a custom route handler is required for what you are doing.
For your "globalized" URIs, a regular MVC route, with a constraint that the "locale" parameter must be equal to "en", "de", etc., will do. The constraint will prevent non-globalized URIs from matching the route.
For a "non-globalized" URI, make a "catch-all" route which simply redirects to the default or cookie-set locale URI.
Place the "globalized" route above the "catch-all" route in Global.asax, so that "already-globalized" URIs don't fall through to the redirection.
You would need to make a new route handler if you want a certain URI pattern to trigger something that is not an action on a controller. But I don't think that's what you're dealing with, here.
You should be able to do this with ASP.NET MVC's default template, I'm doing something similar. Just build your routes as {language}/{controller}/{action}/{id}
Just set a default route that goes to a controller that checks for the language cookie, and redirects the user based on that cookie.

Categories

Resources