简体   繁体   English

ASP.NET MVC-路由

[英]ASP.NET MVC - Routes

I'm working on an MVC application and I have and admin area... So what I need is: 我正在开发MVC应用程序,并且有一个admin区域...所以我需要的是:

When user makes request to admin (for example "/Admin/Post/Add") I need to map this to controller Admin Post and action Add... is it possible? 当用户向管理员发出请求时(例如“ / Admin / Post / Add”),我需要将此映射到控制器Admin Post并执行操作Add ...是否可以?

If your controller is named AdminPostController and you want it to map to '/Admin/Post/Add' then you can use: 如果您的控制器名为AdminPostController,并且您希望它映射到'/ Admin / Post / Add',那么您可以使用:

routes.MapRoute("Admin",  // Route name
  "Admin/Post/{action}/{id}",  // URL with parameters
  new { controller = "AdminPost", action = "Add", id = "" }  // Parameter defaults
);

Note the use of the parameter defaults. 注意使用参数defaults。

If your controller is named AdminController and you just wanted to separate the request method then use the default: 如果您的控制器名为AdminController,而您只是想分离请求方法,则使用默认值:

routes.MapRoute("Default",  // Route name
  "{controller}/{action}/{id}",  // URL with parameters
  new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

Which will map '/Admin/Add/' to the controller: 它将“ / Admin / Add /”映射到控制器:

public class AdminController : Controller {

  [AcceptVerbs(HttpVerbs.Post)]
  public ActionResult Add(int id) {
    //...
  }

  [AcceptVerbs(HttpVerbs.Get)]
  public ActionResult Add(int id) {
    //...
  }

}

Note the use of [AcceptVerbs] to identify which method to invoke for POST requests and GET requests. 请注意使用[AcceptVerbs]来识别要为POST请求和GET请求调用的方法。

See Scott Gu's blog for more details 有关更多详细信息,请参见Scott Gu的博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM