Tuesday, 15 July 2014

asp.net - .net mvc routes with parameter proceeding action -



asp.net - .net mvc routes with parameter proceeding action -

i want know if possible (it seems should be)

i have route such as:

/events/{id}/addcomment

where {id} param used identify event add together comment too. i'm aware simple /events/addcomment/{id} not how want route action i've gotten far looking @ other posts

i register route in global.asax file -

routes.maproute( "addcomment", "events/{id}/addcomment", new { controller = "events", action = "addcomment", id = "" } );

then action within of events controller -

[actionname("{id}/addcomment")] public actionresult addcomment(int id) { event _event = db.events.find(id); var auth = oacontext.loginredirect("must logged in add together comment"); if (auth != null) homecoming auth; else if (_event == null) homecoming httpnotfound(); else homecoming view(_event); }

i've tried , without actionname annotation, not exclusively sure i'm doing wrong here.

later plan have routes such /events/{eventid}/comment/{commentid}/{action} allow users edit/delete comments event, first need figure out how route this.

the reason i'm asking have not seen other samples of parameters proceeding actions in url, perhaps i'm not able , if that'd know.

my question: is url format possible , if proper way code routes , controller?

this route should work:

routes.maproute( "addcomment", "events/{id}/addcomment", new { controller = "events", action = "addcomment" } );

you don't need using actionname attribute on action work. create sure have defined route before default route. notice since {id} route parameter not lastly part of route definition cannot optional anymore. should specify value when generating route.

for example:

@html.actionlink( "click me", "addcomment", "events", new { id = "123" }, null )

asp.net asp.net-mvc-4 asp.net-routing

No comments:

Post a Comment