Asp.Net MVC routing: best way to have one element in url?

I'll give an example of a SO site. To go to the list of questions, the URL is www.stackoverflow.com/questions. Behind the scenes, this goes to the controller (whose name is unknown) and one of its actions. Let's say it's controller = home and action = questions .

How to prevent user from entering pages www.stackoverflow.com/ home /which will result in one page and will result in lower page rankings as SEO. Is a redirect required to resolve this issue? Are there any special routing rules required for this kind of situation? Something else?

thank

+1


source to share


3 answers


I assumed the controller was questions

, and the action was index

, i.e. the default action as defined by the route handler. Thus, there is no alternative path to the page.



+1


source


During a Presentation by Phil Haack of PDC , Jeff shows some source code for. Among the things it shows is the code for some of the route registrations. He got them in controllers and it is not clear to me that he is using the default route. Unless you're using the default route, you don't need to worry about / home / questions, for example.

As for / questions / index, yes, permanent redirection is the way to go. You will not get a search engine penalty for a permanent redirect.



Another way to fix / home / questions is to use route restriction.

+1


source


You want to use the following route. It is very easy to create a new route that eliminates the need for the controller to be on the route. You create a template string that just contains an action, and you default to a controller for the controller you want to use, such as Home.

routes.MapRoute(
    "MyRoute",
    "{action}",
    new { controller = "Home", action = (string)null },
    new { action = "[a-zA-z_]+" }
);

      

Hope it helps.

+1


source







All Articles