问题描述:

I am building a blog engine using MVC 3 and razor. In this scenario, I have given options like a user can have multiple blogs (similar to blogger.com)

Now say a user 'yasser' has the following 3 blogs

  • TechStory
  • GameGeek
  • MeMyStory

so I want all other users to access these blogs by the following urls

  • www.domainName.com/blogs/TechStory
  • www.domainName.com/blogs/GameGeek
  • www.domainName.com/blogs/MeMyStory

And more blogs can be added hence more such url will be acessed in future.

I know that something needs to be done with Routing, but being new to MVC dont seems to get it. Please can some one guide me on this.

网友答案:

Add this route on top of your Default one:

routes.MapRoute(
    "Blog",
    "Blogs/{blogName}",
    new { controller = "Blogs", action = "Index" }
);

Your controller will look like this:

public class BlogsController : Controller
{
    public ActionResult Index(string blogName)
    {
        BlogModel model = // find blog by blog name
        return View(model);
    }
}

Also, one suggestion: Keep your controller names in singular mode: BlogController instead of BlogsController. Change URL and Routing accordingly if you decide to do so.

相关阅读:
Top