ASP.NET MVC URL

My goal is for URL routing to be as follows:

http://www.abc.com/this-is-peter-page http://www.abc.com/this-is-john-page 

What is the easiest way to achieve this without specifying the controller name of the function name in the url above? If the page above is not found, I should redirect to page 404.

Addon 1: this-is-peter-page, and this-john page is not static content, but is from the database.

+7
seo asp.net-mvc
source share
3 answers

Like the KingNestor implementation, you can also perform the following actions to make your work easier:

1) Write your model

 public class MyUser{public String UserName{get; set;}} 

2) add route to global asax

 routes.MapRoute( "NameRouting", "{name}", new { controller = "PersonalPage", action = "Index", username="name" }); 

3) Roll up your own custom mediator from IModelBinder

 public class CustomBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var request = controllerContext.HttpContext.Request; var username = getUserNameFromDashedString(request["username"]); MyUser user = new MyUser(username); return user; } } 

4) in your action:

 public ActionResult Index([ModelBinder(typeof(CustomBinder))] MyUser usr) { ViewData["Welcome"] = "Viewing " + usr.Username; return View(); } 
+9
source share

I personally would not suggest such a route, but if it meets your needs, you need to do something like:

In the Global.asax file, specify the following route:

  routes.MapRoute( "NameRouting", "{name}", new { controller = "PersonalPage", action = "routeByName" }); 

Then in your "PersonalPageController" use the following method:

  [AcceptVerbs(HttpVerbs.Get)] public ActionResult routeByName(string name) { switch (name) { case "this-is-peter-page": return View("PeterView"); case "this-is-john-page": return View("JohnView"); case Default: return View("NotFound"); } } 

Make sure you have the appropriate views: "PeterView", "JohnView" and "NotFound" in your / PersonalPage / view.

+4
source share

I do not think it can be done. AFAIK ASP.NET MVC recognizes routing parameters using the "/" character.

On the other hand, your format is passed using "{controller} -is- {id} - {action}", so it is impossible to distinguish the controller from the identifier and the action.

I think that using the "/" characters does not affect or impair SEO; this only affects the readability and persistence of the URL.

In any case, the following URL is possible: http://www.abc.com/this-is-the-page-of/Peter by adding another route to the Global.asax RegisterRoutes method:

  routes.MapRoute( "AnotherRoute", "this-is-the-page-of/{id}", new { controller = "PersonalPage", action = "Details", id = "" } ); 

... assuming that PersonalPageController implements the Details ActionResult method, which points to the desired page.

+2
source share

All Articles