Asp.net mvc ASP.NETMVC3:创建自定义URL,但不包含确切的控制器名称

Asp.net mvc ASP.NETMVC3:创建自定义URL,但不包含确切的控制器名称,asp.net-mvc,routes,friendly-url,Asp.net Mvc,Routes,Friendly Url,对于一个项目,我必须(不幸地)匹配一些精确的url 所以我认为这不会是一个问题,我可以使用“MapRoute”,将URL与所需的控制器匹配。但我不能让它工作 我必须映射此URL: http://{Host}/opc/public-documents/index.html 到 另一个例子是映射 http://{Host}/opc/public-documents/{year}/index.html 到 我在我的领域(ocpAreaRegistration.cs)尝试了这个,但没有成功: 但我有

对于一个项目,我必须(不幸地)匹配一些精确的url

所以我认为这不会是一个问题,我可以使用“MapRoute”,将URL与所需的控制器匹配。但我不能让它工作

我必须映射此URL:

http://{Host}/opc/public-documents/index.html

另一个例子是映射

http://{Host}/opc/public-documents/{year}/index.html

我在我的领域(
ocpAreaRegistration.cs
)尝试了这个,但没有成功:


但我有一些404错误:(当我试图访问它时。我做错了什么?

我不知道为什么你需要这样做(我只能假设你来自一个遗留应用程序),但这对我来说是可行的:

opcAreaRegistration.cs:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "opc_public_year_docs",
        "opc/public-documents/{year}/index.html",
        new { controller = "Documents", action = "DisplayByYear" }
    );

    context.MapRoute(
        "opc_public_docs",
        "opc/public-documents/index.html",
        new { controller = "Documents", action = "Index" }
    );

    context.MapRoute(
        "opc_default",
        "opc/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}
控制器:

public class DocumentsController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult DisplayByYear(int year)
    {
        return View(year);
    }
}

确保您将这些路由放在区域路由文件而不是global.asax中,这样您就可以开始了。

何时调用OCParareRegistration?它应该由global.asax.cs中的
RegisterRoutes调用。您不能对两条不同的路由使用相同的maproute id。实际代码中是这样的吗?@cellik对不起mapRoute Id,仅在示例中,复制错误-paste@podiluska因为它是文件(创建区域时由VS2010创建)它注册了opc区域的默认路由,我想它应该已经被调用了。我试图将此代码直接放在Global.asax.cs中,但它没有改变任何东西。哎哟,我不知道ordre有一些重要性,你的代码指出了这一点!在我的自定义规则之前,我有默认路由(opc_default)。非常感谢。(原因是:这是一个联邦网站,他们在大约两个月的时间里讨论了如何定义url结构,所以我必须使用他们的url。@J4N不客气,这听起来很有趣。:)至于路由,正如您已经注意到的,它确实从具体到一般。如果你对这个例子感兴趣,我昨天发布了一个类似的问题,希望能有所帮助。
context.MapRoute("DocumentsIndex", "opc/public-documents/index.html", 
    new {area="opc", controller = "Documents", action = "Index"});
context.MapRoute("DocumentsDisplayByYear", "opc/public-documents/{year}/index.html", 
    new {area="opc", controller = "Documents", action = "Action:DisplayByYear"});
public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "opc_public_year_docs",
        "opc/public-documents/{year}/index.html",
        new { controller = "Documents", action = "DisplayByYear" }
    );

    context.MapRoute(
        "opc_public_docs",
        "opc/public-documents/index.html",
        new { controller = "Documents", action = "Index" }
    );

    context.MapRoute(
        "opc_default",
        "opc/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}
public class DocumentsController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult DisplayByYear(int year)
    {
        return View(year);
    }
}