I would like to make my app url like http://domain.com/product/product-name....
Is it possible that is if I am passing productid and can get productname in my url?
URL Rewriting is the practice of "faking" a URL by rewriting it to look like another URL. It does so in a single direction. This means it is has no built-in way to generate a URL for use on the UI.
.NET Routing is what MVC applications typically use. It is a real, not faked 2-way mapping from URL to a set of route values and/or a set of route values to a URL. This means that you can easily generate URLs for use within the application using HTML helpers such as ActionLink, and as such is the recommended and preferred approach for building custom URLs from within the application.
Sometimes both of these techniques are used, but URL rewriting is generally only put in place to do things that make sense to change from outside of the compiled application, such as when nesting the application within another application or putting in one-way 301 redirects for legacy URLs.
To answer your question, yes it can be done, but should be done with .NET routing, not URL rewriting. To use names like this, you need a mapping between name and ID. A couple of ways this could be done:
Make a Name-ID mapping right in your route configuration by using static route segments with the name and route values that contain the related ID. This approach will work only if your URLs don't need to be changed dynamically within the application.
Use a Name-ID map within a cached dictionary object. Use a custom RouteBase implementation to do mapping in each direction by overriding the GetRouteData and GetVirtualPath methods.
Related
Within an MVC5 app we have introduced a security model which will authorise the user logged in against the requested Url. I have been looking at ways to auto generate a list of available Urls within the application on start up.
I first entertained the idea of using reflection to build up relative Urls from the area/controller/action names similar to this post
This will work if all the urls match the standard convention within MVC however; we have some routes that do not match this convention so an auto scan using reflection will not cover all grouds.
Is there a way (on application_start event) I can possibly use the route table to determine what Urls are available?
Note:
I understand the Http Context is not yet available at this point but I do not need the whole Url, only the relative url after the domain.
I am currently working with localizing my MVC4 web application and have run in to an issue. My site is in four languages, English, French, Russian and Polish. I set up the sites culture based upon what the user selects. This is not tracked in the url.
I want my urls to be SEO friendly. So I need them to be localized. The tricky part though is that fact I routes set up for my controllers in English. For example
/product/product-name is mapped to the product controller and the get action
/customer/details is mapped to the customer controller and the details action
How can I localize the routes to different languages/cultures? So when I create an action link it maps to correct controller/action, generating the localized url?
I found this solution and it is very elegant but the issue I have is that it does not work as well for dynamic url's, as there is a need for explicit mapping.
Actually I don't know the right answer for that question and it's really very interesting how it possible to do that with standard MVC routing.
But if you do not find proper way to implement that with standard options you could implementing that by hands with the helps of front controler.
You create the class which will be main point of availability inside your app. And inside that method you could implement redirecting logic which will redirect you to the proper controller/action.
What is the benefit of using Url.RouteUrl or Url.Action versus just using the URL directly?
If you change your routing configuration, by using Url.RouteUrl or Url.Action your generated Url's will update along with your routing configuration.
I find the real benefit comes when tied together with T4MVC. Then I have strongly typed access to my Action routes, so if my controller's change the compiler alerts me if any of my Url's need to change as well.
It's very simple to set up custom routing rules to create so-called "pretty" (or SEO-friendly) URLs. If you ever change one of those URLs, you don't want to have to go through your entire application and change it every time you link to that action. On the other hand, if you use Url.Action, it will change automatically.
What are extensibility points of URL generation in ASP.NET MVC?
Routes - virtual path depends on it
???
Addendum 1
Particularily, I need to control not only path part of URL but the host also. And I'd like to embed my generation logics into MVC Framework so that any call to standard Html.ActionLink method would involve my logics. It would be perfect. However, investigating MVC sources I'm desperate to achieve my goal with an easy way.
Routes is pretty much where it's at. Remember, though, that you can subclass Route and provide your own implementation that does not use key/value URI templates.
There's nothing stopping you from writing your own solution from the ground up, but there's not much point since there's already an extensible foundation for you to work with.
Routing extensibility points
Routes
Route constraints
Route handlers
In your particular case, you will have to write your own route that will populate additional RouteData items related to host. Parsing your URL will be totally on you.
"{host}/{controller}/{action}"
Maybe also create your custom route handler that will make host parameter mandatory. But that already depends on your particular needs.
Edit
I guess that this article about domain routing may be of some help to you. It looks preety straight forward and uncompicated.
Indeed, it's impossible to reach desired flexibility since Route gives just VirtualPath portion during URL generation.
I'd like to have a routing rule that accepted part of my domain name as a parameter. For example:
{name}.mydomain.com/photos/{id}
Is this even possible?
I have had a similar issue in using asp.net mvc, but with using the whole domain instead of just the subdomain. What we used was a custom route constraint to determine which controller to go to (domain determined controller on our project). Then in the controller we used the normal asp.net request.url properties to take an action. This may or may not help depending on your exact requirements.
It wouldn't be possible since the {name}.mydomain (a valid name instead of {name}) is the Authority part of a Uri. The routing can be performed only on the PathAndQuery part of a Uri.
Edit: I was somehow wrong, take a look at this answer: Manipulating Url structure
See ASP.NET MVC Domain Routing by Maarten Balliauw.