如果我们有“ example_name”,则可以使用[ActionName(“”)]在url中进行更改。因此,我要为控制器名称执行此操作。
我可以这样做:
ControllerName> example_nameController>在URL中:“ / case_studies / adaep”
我想在URL中这样更改控制器名称:“ / case-studies / adaep”
[RoutePrefix("case-studies")]
public class case_studiesController : Controller
{
// GET: CaseStudies
[Route]
public ActionResult Index()
{
return View();
}
[HttpGet]
[Route("adaep")]
public ActionResult adaep()
{
return View("/views/case-studies/pagename.cshtml");
}
}
我这样做是这样,但仍不能使用/example_controller
的控制器名称
答案 0 :(得分:3)
全局启用路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();
// routes.MapRoute...
}
创建一个自定义路由处理程序,如本博客所示:
public class MyRouteHandler : MvcRouteHandler{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
return base.GetHttpHandler(requestContext);
}
}
...以及新路线:
routes.Add(
new Route("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "Default", action = "Index", id = "" }),
new MyRouteHandler())
);