[英]C# MVC - namespace in multi controller
I want use AnotherController name in SomeController. 我想在SomeController中使用AnotherController名称。 But, RoutePrefix Attribute is can only be declared by Controller level. 但是,RoutePrefix属性只能由控制器级别声明。
Prepare the following. 准备以下内容。
namespace KRSMART.Controllers
{
public class SomeController : Controller
{
/* localhost:0000/Some/Index */
public ActionResult Index()
{
return View();
}
/* I want Url */
/* localhost:0000/Another/Test */
[Route("Another/Index")]
public ActionResult Test()
{
return View();
}
}
}
It didn't work as I wanted it to. 它没有按我想要的那样工作。
I know I can create a new controller and do it, but I didn't want to. 我知道我可以创建一个新的控制器并执行此操作,但是我不想这么做。
I'd like to get some advice from you who are familiar with Route. 我想从熟悉Route的您那里得到一些建议。
You need to add routes.MapMvcAttributeRoutes();
您需要添加routes.MapMvcAttributeRoutes();
before default route register, 在默认路由寄存器之前,
your RegisterRoutes
function should be like, 您的RegisterRoutes
函数应该像
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.