我正在尝试使用路由模块将我的域http://www.example.com/的根路径设置为PersonSearch
控制器,但它似乎没有任何效果(404错误)。
网址http://www.example.com/person/search正确地将我带到了所需的页面。
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"Root",
"",
defaults: new { controller = "Person", action = "Search" }
);
}
}
PersonController.cs
public class PesonController : Controller
{
[HttpGet]
[Route("person/search")]
public ActionResult Search()
{
PersonSearchViewModel psvm = new PersonSearchViewModel();
return View(psvm);
}
}
答案 0 :(得分:0)
我认为它缺少路线中的url参数
routes.MapRoute(
"Root",
url: "{controller}/{action}",
defaults: new { controller = "Person", action = "Search" }
);
答案 1 :(得分:0)
这是一个订购问题。如果您将URL定义为空字符串(根),则路径应放在之前所有其他路由。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Root",
"",
defaults: new { controller = "Person", action = "Search" }
);
routes.MapMvcAttributeRoutes();
// Place any other `MapRoute` declarations here
}
}
答案 2 :(得分:0)
我发现我可以使用简单的属性路由代码实现我想要的目标。
goLineDown
然后,我可以移除public class PesonController : Controller
{
[HttpGet]
[Route("~/")]
[Route("person/search")]
public ActionResult Search()
{
PersonSearchViewModel psvm = new PersonSearchViewModel();
return View(psvm);
}
}
routes.MapRoute()
的号召