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.
Related
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.
Im just getting into Global.asax and the way to do url routing in WebForms. Here Im having a bit of a conflict with my default.aspx and my other files.
I want all my files to have the friendly url ie. mysite.com/welcome/ etc and Im achieving this by doing:
routes.MapPageRoute("root_pages", "{file}/{*action}", "~/{file}.aspx");
by this I can write mysite.com/welcome.aspx into mysite.com/welcome/ and have a default action if I want. But then my conflict occurs between my default routing:
routes.MapPageRoute("default", "{*action}", "~/default.aspx");
I also want to access some action on my default.aspx - but it seems I cant when Im doing it like this?
It will pick the file line and go with that, so I cant do mysite.com/logout/ which is a function on my default.aspx page, it will ofcouse look for a file in this case.. Is there any other way to do what I want? So I can use both routes?
Hope you can help me out
Kind regards
It will always take the first route and go with it. In your case, it will look for logout.aspx which doesn't exist. The only option is to use the URL mysite.com/default/logout.
We can consider this as a limitation of URL Routing in WebForms.
You can check my blog series for URL Routing in Web forms at following URL.
http://karmic-development.blogspot.in/2013/10/url-routing-in-aspnet-web-forms-part-1.html
http://karmic-development.blogspot.in/2013/10/url-routing-in-aspnet-web-forms-part-2.html
There are more articles in this series.
I have a subdomain that is http://trade.businessbazaar.in . I am dynamically creating urls from database something in this manner http://trade.businessbazaar.in/mycompany. To display details, I have an index.aspx file there,thinking that on every request the index.aspx page will load and display data accodingly. Also, There is a masterpage on the index.aspx page from where i am capturing the text mycompany and query it in database to fetch result. But nothing seems to work.
A genuine link is http://trade.businessbazaar.in/Symparlife. But its unable to load index.aspx. I need a clean approach without any third party dll or rewriters. Directly to push some lines in config and start working. That is url will be the same but index page will get loaded...
In short, i want to say
I need the StackOverflow type clean url mechanism to fetch pages
Thanks in Advance
You can handle the Begin_Request event in Global.asax and add custom code to redirect to index.aspx and convert the parts of the URL into query string arguments. You should use Server.Transfer to keep the URL in the browser.
I'd recommend upgrading to 4.0 and using the Routing enine though. You should check if the standard routing is available as a download for ASP.NET 3.5. I am sure your code will get messy very soon. Been there, done that.
As #Mike Miller mentions in the comments the Routing engine ships with ASP.NET 3.5. You can check the documentation here - http://msdn.microsoft.com/en-us/library/system.web.routing(v=vs.90).aspx
Here is a tutorial on how to use it with Web Forms - http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
For your case the code would be something like:
routes.MapPageRoute("company-index", "/{company}", "~/index.aspx")
And in index.aspx you can access the route value for company like this:
string company = (string)Page.RouteData.Values["company"];
Keep in mind that you'd better add something in the URL before your actual argument (the company name). If you don't you will have problems later on when because you may want to add a URL like "/Login" but then you will have to validate that users can't create a company named "Login". Not how Stack Overflow has "/questions/" before the actual question info in the URL.
I'm remaking a website, previous build on DotNetNuke. I need to keep the database, as old URLs working.
The issue: translate the old URL
eq.
http://localhost:8069/Noticias/tabid/78/language/pt-BR/nid/2267/Palestra-
discute-o-futuro-profissional-das-crianca.aspx
to the new mapping engine, that would be something like that:
eq.
http://localhost:8069/{controller}/{action}/{id}/{title}
where, in the example, 2267 is my id, and 78 is the module id that i have to 'translate' for a controller name. The action is view by default.
Any suggestions?
Simply make a base page handler for the requests you want to transfer, and then do a simple Response.Redirect to the appropriate URL on your new website then. All your pages inherit these changes, and you can provide a listing of URLs in a configuration file or database for the URLs to dynamically redirect.
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).