路由前缀不适用于索引

Route prefix is not working with index

我有控制器: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 时,它会自动打开索引页面。

我们将不胜感激。

您可以将参数设为可选

[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 }
    );
}

引用Attribute Routing in ASP.NET MVC 5