2

For a project I've to(unfortunately) match some exact url.

So I thought it will not be a problem, I can use the "MapRoute", to match urls with the desired controller. But I can't make it work.

I've to map this URL:

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

to

Area: opc
Controller: Documents
Action: Index

Another example is to map

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

to

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

I tried this, whitout success, in my area(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"});

But I got some 404 Errors :( when I'm trying to access it. What am I doing wrong?

4
  • When does ocpAreaRegistration get called? It should be called by RegisterRoutes in global.asax.cs Commented Aug 6, 2012 at 12:25
  • You can't use the same maproute id for two different routes. Is it like that in the actual code? Commented Aug 6, 2012 at 12:28
  • @cellik Sorry for the mapRoute Id, it's only in the example, bad copy-paste Commented Aug 6, 2012 at 12:28
  • @podiluska Since it's the file(created by VS2010 when creating the area) which register the default route for the area opc, I guess it should already be called. I tried to put this code directly in Global.asax.cs, but it didn't change anything. Commented Aug 6, 2012 at 12:31

1 Answer 1

2

I'm not sure why you need to do this (I can only assume you're coming from a legacy application), but this is working for me:

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

Controller:

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

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

Make sure you put those routes in the area routing file rather than global.asax and you should be good to go.

Sign up to request clarification or add additional context in comments.

2 Comments

Ouch, I didn't know that the ordre was having some importance and your code point it out! I was having the default route(opc_default) before my custom rule). Thank you!(and for the reason: It's a federal website, and they discussed during something like two month to define url structure, so I've to use their URL.
@J4N You're welcome, and that sounds interesting. :) As for the routing, it does go from specific to general, as you've noticed already. If you're interested in an example of why that's the case, I posted an answer to a similar question yesterday that will hopefully help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.