Note that there are some explanatory texts on larger screens.

plurals
  1. PO.net mvc routes with parameter proceeding action
    primarykey
    data
    text
    <p>I want to know if this is possible (it seems like it should be)</p> <p>I would like to have a route such as:</p> <p>/events/{id}/addcomment </p> <p>where the {id} is a param to be used to identify the event to add a comment too. I'm aware that I could simple do /events/addcomment/{id} but this is not how I desire to route this action so this is what I've gotten so far by looking at other SO posts</p> <p>I register a route in my global.asax file -</p> <pre><code> routes.MapRoute( "AddComment", "Events/{id}/AddComment", new { controller = "Events", action = "AddComment", id = "" } ); </code></pre> <p>Then my action inside of my events controller -</p> <pre><code>[ActionName("{id}/AddComment")] public ActionResult AddComment(int id) { Event _event = db.Events.Find(id); var auth = OAContext.LoginRedirect("Must be logged in to add a comment"); if (auth != null) return auth; else if (_event == null) return HttpNotFound(); else return View(_event); } </code></pre> <p>I've tried this with and without the ActionName annotation, not entirely sure what I'm doing wrong here.</p> <p>Later I plan to have routes such as /events/{eventId}/comment/{commentId}/{action} that will allow users to edit/delete their comments from an event, but first I need to figure out exactly how to route this.</p> <p>The reason I'm asking this is I have not seen any other samples of parameters proceeding actions in the url, so perhaps I'm just not able to do this and if so than that'd be good to know.</p> <p>My question: <strong>is this url format possible and if so what is the proper way to code this into the routes and controller?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload