C# 路由前缀不能与索引一起使用
我有控制器:C# 路由前缀不能与索引一起使用,c#,asp.net-mvc,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc Routing,我有控制器:DoomPlaceController 在路径中:我使用了:doom place/{parameter}//无操作 在控制器中: [Route("doom-place/{parameter}")] public ActionResult Index(string parameter) { return View(); } 我想要的:当我点击网址:www.xyz.com/doom place 它应该打开doomplace/index页面 但现在,我可以使用doom-plac
DoomPlaceController
在路径中:我使用了:doom place/{parameter}//无操作
在控制器中:
[Route("doom-place/{parameter}")]
public ActionResult Index(string parameter)
{
return View();
}
我想要的:当我点击网址:www.xyz.com/doom place
它应该打开doomplace/index
页面
但现在,我可以使用doom-place/index
访问该页面,但我希望当我点击www.xyz.com/doom-place
时,它会自动打开索引页面
非常感谢您的帮助。您可以将参数设置为可选参数
[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 }
);
}
参考使参数可选它是可选的。我对参数没有问题。我只是不想在URL
中显示索引,但现在,我可以使用doom place/index
访问页面,当您这样做时,参数的值是多少?我调试并获得了值“index”,然后根据操作的当前路由模板,该值是准确的。是该操作/视图所必需的参数。如果没有,那么考虑删除它。我是否应该更改我的视图/ DOMOSITE文件夹名?毁灭-place@SiddharthRajput否,框架将相应地映射,因为它使用实际的控制器名称DoomPlaceController
映射视图位置views/doomplace/index.cshtml
。不是路由prefixit给我的错误,无法调用操作方法'System.Collections.Generic'。List@SiddharthRajput如果你能花点时间查看答案中包含的参考链接,这样你就可以更好地了解需要做什么。谢谢。这对我很有帮助:)现在工作了