0

i have a Post Controller that should handle routes like these:

mywebsite.com/post/somthing
mywebsite.com/post/post1
mywebsite.com/post/anotherpost
...

when i searched online for my answer i found how to register my map route like this:

routes.MapRoute(
    name: "post",
    template: "post/{*postname}",
    defaults: new { controller = "Post", action = "ReadPost" });

but i don't know that when i create a controller and action method for my route how to take the {*article} as an input for my method so that method know what model to return with view.

also my controller should have to handle following routes as well:

mywebsite.com/post/anotherpost/comments
mywebsite.com/post/anotherpost/edit
mywebsite.com/post/anotherpost/author

when the route ends with nothing it should be redirected to ReadPost(string postName)

when the route ends with comments it should be redirected to ReadComment(string postName)

when the route ends with edit it should be redirected to Edit(string postName)

and when the route ends with author it should be redirected to ReadAuthor(string postName)

how should i write the controller and routemapping so that proper route connect to proper action and give the method the "post name" as input?

4
  • You wrote, "...how to take the {*article} as an input..." What is article? Commented Oct 5, 2016 at 15:18
  • is {antherpost} in your question is the string postName? Commented Oct 5, 2016 at 15:29
  • @ShaunLuttin sorry, that was a typo i fixed it Commented Oct 5, 2016 at 17:24
  • @Liran yep, it's a string name. Commented Oct 5, 2016 at 17:25

1 Answer 1

1

I think you mixed the route a bit, you should not name your controller like the HTTP verb its going to use, its the other way around, you should use http://www.domain.com/article and create actions in your controller to match the HTTP verbs like so (im using web api cause i had a project opened already):

public class ArticleController : ApiController
{
    // GET: api/Default
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET: api/Default/5
    public string Get(int id)
    {
        return "value";
    }

    // POST: api/Default
    public void Post([FromBody]string value)
    {
    }

    // PUT: api/Default/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE: api/Default/5
    public void Delete(int id)
    {
    }
}

and then you can use routes in apiconfig.cs

like this:

 config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

here are some REST best practice hope this will help.

EDIT:

just to clarify i dont like what i done here and to my opinion its all kinds of wrong but to help you get started here is the controller code:

public class PostController : Controller
{

    public ActionResult ReadPost(string postName)
    {
        return View("~/views/home/Index.cshtml");
    }

    public ActionResult Comments(string postName)
    {
        ViewBag.Message = "Your application description page.";

        return View("~/views/home/Index.cshtml");
    }

    public ActionResult Edit(string postName)
    {
        ViewBag.Message = "Your contact page.";

        return View("~/views/home/Index.cshtml");
    }

    public ActionResult Author(string postName)
    {
        ViewBag.Message = "Your contact page.";

        return View("~/views/home/Index.cshtml");
    }

}

and here is route file :

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //routes.MapRoute(
        //    name: "Default",
        //    url: "{controller}/{action}/{id}",
        //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        //);




        routes.MapRoute(
           name: "postOnlyParam",
           url: "post/{postName}",
           defaults: new { controller = "Post", action = "ReadPost", postName = UrlParameter.Optional }
       );

        routes.MapRoute(
          name: "post",
          url: "post/{action}/{postName}",
          defaults: new { controller = "Post", action = "ReadPost", postName = UrlParameter.Optional }
      );


    }
}

i have tested it and does what you want if i understand your question.

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

2 Comments

not the answer i was looking for. the code segment you are referring to was coppiend from this link which is i slightly modified to point out the fact about how i am planning on modifying my code and mentioned that the code mapping i putted there would only work to answer the first part of my question not the second part, i fixed the article name issue anyway. the controller and mapping method you putted does the same job as the default mapping in MVC core. which i don't need here
@soorena12 check the answer now please.

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.