1

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.

1 Answer 1

3

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.

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

Comments

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.