2

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

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

我必须映射这个 URL:

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

Area: opc
Controller: Documents
Action: Index

另一个例子是映射

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

Area: opc
Controller: Documents
Action:DisplayByYear
Year(Parameter): {year}

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

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

但是我遇到了一些 404 错误 :( 当我尝试访问它时。我做错了什么?

4

1 回答 1

2

我不确定你为什么需要这样做(我只能假设你来自遗留应用程序),但这对我有用:

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 中,这样就可以了。

于 2012-08-06T12:30:38.860 回答