【问题标题】:Remove controller name from URL with Attribute routing使用属性路由从 URL 中删除控制器名称
【发布时间】:2016-02-18 05:16:54
【问题描述】:
【问题讨论】:
标签:
c#
asp.net
asp.net-mvc
routing
【解决方案1】:
Gabriel 的回答是正确的,但是,它可能会有点误导,因为您要求的是 MVC,而该答案是针对 Web API。
无论如何,您想要的是将注释放在类定义而不是操作方法上。 MVC 示例如下:
[RoutePrefix("SomethingOtherThanProduct")]
public class ProductController : Controller
{
public ActionResult Index()
{
...
return View();
}
}
我也放弃了这个作为答案,因为您可能会发现以下文章有帮助:[Attribute] Routing in ASP.NET MVC 5 / WebAPI 2
【解决方案2】:
确保在整个控制器类上设置 RoutePrefix 属性,并在操作上使用 Route 属性。
[RoutePrefix("notproducts")]
public class ProductsController : ApiController
{
[Route("")]
public IEnumerable<Product> Get() { ... }
}