c# - Route is matching on parameter, not action name / View isn't returning properly -


i want create url below:

www.mywebapp.com/users/profile/john 

i have userscontroller controller , profile action, returns viewresult profile page.

i've created route manage it:

routes.maproute(     name: "profileroute",     url: "users/profile/{username}",     defaults: new { controller = "users", action = "profile", username = urlparameter.optional } ); 

the first question is: if change {username} {id}, works. when put {username} parameter, action gets null in parameter. why this?

here's action called profile:

[httpget] public actionresult profile(string id) {     if (usersrepository.getuserbyusername(id) == null) {         return partialview("~/views/partials/_usernamenotfound.cshtml", id);     }     return view(id); } 

i've added view page show user profile. however, when method ends execution, got error:

the view 'john' or master not found or no view engine supports searched locations.  following locations searched: ~/views/users/john.aspx ~/views/users/john.ascx ... 

the second question is: the page have show profile, not page username's name. why happening?

you getting error because passing string (id) view function, overload searches view name passed in string (in case username).

if trying pass username directly view can use viewbag, code should this:

public actionresult profile(string id) { if (usersrepository.getuserbyusername(id) == null) {     return partialview("~/views/partials/_usernamenotfound.cshtml", id); } viewbag.username=id; return view(); } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -