c# - ASP.NET MVC - Write less code for route template -


this code:

routes.maproutelowercase(                 name: "productadd",                 url: "product/add",                 defaults: new                 {                     controller = "product",                     action = "add"                 }                     , namespaces: new[] { "project.controllers" });             routes.maproutelowercase(                 name: "productlike",                 url: "product/like",                 defaults: new                 {                     controller = "product",                     action = "like"                 }                     , namespaces: new[] { "project.controllers" });             routes.maproutelowercase(                 name: "productshow",                 url: "product/{id}/{seoname}",                 defaults: new                 {                     controller = "product",                     action = "get",                     id = urlparameter.optional,                     seoname = urlparameter.optional                 }                     , namespaces: new[] { "project.controllers" }); 

i want solution writing less codes, template productshow , template product actions

you can use attribute based routing in mvc. available default in mvc5, or can installed nuget package in mvc4.

with attribute based routing, can define attributes on action methods, rather magic string matches in routing table. can perform more advanced type checking, such minimum , maximum values, , optionally name routes easy reference in razor.

as example:

[routeprefix("product")] public class productcontroller : controller {  //route /product [route]  public actionresult index() { ... }   //route /product/add [route("add")] public actionresult add() { ... }  //route /product/like // <a href="@url.routeurl("productlike")">like</a> [route("like", name="productlike")] public actionresult like() { ... }  //route /product/{id}/{seoname} [route("{id?}/{seoname?}")] public actionresult get(int? id, string seoname) { ... }  } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -