1

I am new to both Web development and asp.net mvc and i am trying to create a blog as my first project.

Now as is the norm that in blogs every post has its own page(sort of like stackoverflow having a new page for each question). But i am having a hard time comprehending that how i am going to achieve this.

Because for example every new page must have its own view and its own action method. Now suppose if there are 1000 blogposts that would that mean 1000 views and 1000 actions in the controller being created dynamically.

Surely there must be some other way. A little guidance in this matter would help alot.

2 Answers 2

2

You will only have one action and one view, but different data (view model) for different blog posts. So, for example, let's say you declare a special route for your blog posts:

routes.MapRoute(
    "BlogPostDetails",
    "posts/{id}/{title}",
    new { controller = "Posts", action = "Details" }
);

Here I'm specifying an additional URL parameter called title to make URLs more SEO-friendly (e.g. "/posts/1/Hello%20world").

Next thing is to define a model and controller:

// /Models/BlogPost.cs
public class BlogPost
{
    public string Heading { get; set; }
    public string Text { get; set; }
}

// /Controllers/PostsController
public class PostsController : Controller
{
    public ActionResult Details(string id)
    {
        BlogPost model = GetModel(id);

        if (model == null)
            return new HttpNotFoundResult();

        return View(model);
    }

    private BlogPost GetModel(string blogPostId)
    {
        // Getting blog post with the given Id from the database
    }
}

And finally this is how your view (/Views/Posts/Details.cshtml) should look like:

@model [Root namespace].Models.BlogPost;

<article>
    <h2>@Model.Heading</h2>
    <p>@Model.Text</p>
</article>

Hope this clarifies things a bit for you.

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

Comments

1

You'll have a parameter to your action method that identifies the actual blog post.

So for example:

/post/view/123

would view blog post with ID 123. Your action on PostController would look like

ViewResult View(int postId){
    //get from db, return appropriate content via view here
}

So you only need one controller, and one action in this example to do all this. Just the parameter changes.

2 Comments

and what about the customer uri(like stackoverflow has for each question)?
You'll notice that if you remove the last bit of a SO URL (leaving the int id), it still works. The last bit is mostly just there for SEO, and doesn't have an impact on routing. To make something like that work in your own app, you need a wildcard in your route. Refer to the {*pathInfo} bit in this question to see how that's done: stackoverflow.com/questions/10344986/mvc3-routing-basics . Note that SO will redirect you to the full URL if you just enter the int id bit. It gets the title out of the db, based on the int id and does the redirect.This is so that there is a "canonical" url.

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.