2

I come from a Rails background and I'm having problems wrapping my head around Microsoft's MVC framework.

Today it's Routing. Rails gives you namespaces (e.g. Admin) which is the equivalent of Areas in .NET MVC3. Rails also allows you to define nested resources within your routes that will give you for example /posts/1/comments/1/edit and in your action you basically get params[:post_id] and params[:id].

I need something similar in ASP.NET MVC3 but not sure how to go about this. Googling for this results in at least 30 different ways to accomplish this and non of them mention areas.

It feels like I should add/modify something within here:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

But not sure where. Any suggestions?

1 Answer 1

2

I think you're in the right file (your AreaRegistration.cs file). I prefer being a little more explicit with my routes rather than using the default 'catch all' type of route that they provide. So here's an example of how I'd handle this:

Add something like this before the existing route (or get rid of the existing one all together) in the RegisterArea method

context.MapRoute(
    "Edit_Comment",
    "posts/{postId}/comments/{commentId}/edit",
    new { controller = "Comment", action = "Edit" }
);

Then in your CommentController.cs you would have the following action:

public ActionResult Edit(int postId, int commentId)
{
    // Do your edit logic then return an ActionResult
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, I will give this a shot although it looks like it should work. Have you worked with Resources in MVC3 and areas? i.e. - stevehodgkiss.com/posts/restful-routing-for-asp-net-mvc-2
I haven't worked with resources in mvc3. With the way our urls are structured it wouldn't be a real good fit. We pass most of our data around via get or post parameters rather than using route values.

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.