我有控制器:DoomPlaceController
In route: I have used: doom-place/{parameter} // no action
在主计长中:
[Route("doom-place/{parameter}")]
public ActionResult Index(string parameter)
{
return View();
}
我想要的:当我点击URL:www.xyz.com/doom-place
时,它应该打开Doom-Place/index
页面。
但是现在,我可以用doom-place/index
访问页面,但是当我点击www.xyz.com/doom-place
时,它会自动打开索引页。
我们会感谢你的帮助。
发布于 2017-12-23 05:45:32
可以将参数设置为可选的。
[RoutePrefix("doom-place")]
public class DoomPlaceController : Controller {
//Matches GET /doom-place
//Matches GET /doom-place/some_parameter
[HttpGet]
[Route("{parameter?}")]
public ActionResult Index(string parameter) {
return View();
}
}
在使用属性路由的情况下,假设属性路由已在RouteConfig.RegisterRoutes
中启用。
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
//enable attribute routing
routes.MapMvcAttributeRoutes();
//covention-based routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
https://stackoverflow.com/questions/47950019
复制相似问题