Reputation: 730
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?
Upvotes: 0
Views: 2594
Reputation: 611
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.
Upvotes: 1