Asp.net mvc routing guid id为的默认mvc 5路由

Asp.net mvc routing guid id为的默认mvc 5路由,asp.net-mvc-routing,asp.net-mvc-5.1,Asp.net Mvc Routing,Asp.net Mvc 5.1,我正在尝试设置MVC5属性路由,以便 https://localhost/19a6de7e-ee19-43f5-a9c9-8bbdc8dcfc5e 路由到/home/index初始化id参数,我似乎不能完全正确 我添加了routes.mapmvcattributerroutes();在默认映射路由之后注册路由 routes.MapRoute( name: "Default", url: "{controller}/{action}

我正在尝试设置MVC5属性路由,以便

https://localhost/19a6de7e-ee19-43f5-a9c9-8bbdc8dcfc5e
路由到/home/index初始化id参数,我似乎不能完全正确

我添加了routes.mapmvcattributerroutes();在默认映射路由之后注册路由

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
我已经尝试了很多方法,但除非路线完全确定,否则似乎无法让它发挥作用

有什么建议吗

public class HomeController : Controller
{
    [Route("{id?}")]
    public ActionResult Index(Guid? id)
    {
        ViewBag.Message = id;
        return View();
    }
}

@chrisp_68,您需要在默认路由之前放置一个自定义路由:

routes.MapRoute(
    name: "Just ID",
    url: "{id}",
    defaults: new { controller = "Home", action = "Index" }
);
然后,您需要创建一个以id为参数的操作:

public ActionResult IndexID(string id) {
    ...
}

您可以考虑的另一件事是将ID的值约束为GUID模式:

    constraints: new { id = @"[a-f0-9-]+" }

(或您需要的任何模式)

现在添加了一个内置的guidroteconstraint(),它比使用正则表达式更可取