views:

962

answers:

1

I am creating a directory of sorts with members and their profiles. I'm using the MVC framework in .net.

I have a view that allows you to find members based on some criteria so my controller has a Find() action result, then another that accepts the post verb. So, somesite.com/members/find displays the search tools, then once the form has been submitted the same url displays the results. I now want the member name to link to another actionresult method in the same controller for displaying their profile and i want the url's to follow this pattern somesite.com/members/{username}.

How do i create a controller method that will do this? Is there more to it than that?
Do i need to map new routes in the global.asax?

Thanks for the help.

+3  A: 

First create your action method on the controller like this:

public ActionResult Profile(string userName)
{
    // Do What you want with the userName
    throw new System.NotImplementedException();
}

Then create a new route in your global.asax file to handle the somesite.com/members/{username} like this:

routes.MapRoute(
    "Profile",
    "members/{username}",
    new { controller = "Members", action = "Profile", userName = "" }
);

That's it. I am not sure what you are calling the controller that will host the action method. I assumed members.

Dale Ragan
Thanks for your quick reply... Is their a correlation between the name of the maproute and the actionResult method name (i.e. Profile - Profile)?
Jeremy
No, that is just the name I gave the route.
Dale Ragan
I added the two things you suggested but get any results after trying it out. browsed to somesite.com/members/someuser and the profile action was not called... simply received a "The resource cannot be found" message. Thanks again
Jeremy
What is the controller's name that you added the action to? I did a quick test before posting my answer and it worked fine.
Dale Ragan
my controller is "Vendor" class name VendorController... each vendor should have a profile. So in the global.asax i added a route named "Profile" with a url of "vendor/{username}" and "new { controller = "Vendor", action = "Profile", userName = "" }" as the defaults
Jeremy
my implementation of the action is just as you have it...
Jeremy
Awh, I see the problem. In your comment above you indicated you browsed to somesite.com/members/someuser, but your url for the route you just gave me is "vendor/{username}". If you want to use members/someuser, then change the url back to "members/{username}".
Dale Ragan
Otherwise, navigate to somesite.com/vendor/someuser and that will call the action as you have it setup.
Dale Ragan
Sorry for the confusion, I was initially using "members" but changed it all to "Vendor", i am now testing with vendor/username. So i want to be able to go to somesite.com/vendor/somevendorsname for example. Thanks
Jeremy
And it still doesn't seem to work? I am testing in vs2008 dev server and have closed and allowed the app start to fire again, but still no luck? Any idea.
Jeremy
OK, so you got it working?
Dale Ragan
You need to close the ASP.NET Development Server that was created when you started debugging, by right clicking the icon in the tray and clicking Stop. Once you do that, debug the app again.
Dale Ragan
Should this be the only route... I added this to collection of routes. So currently there is the default route ( "{controller}/{action}/{id}" ) and the one i just added.
Jeremy
Restarted the dev server and still having the same results. :(
Jeremy
The profile route you just added should be above the one you just posted in your comment.
Dale Ragan
ok... that seems to have been the problem... although i now have a new one. I think i can solve it though. It seems the default route conflicts with this one in that somesite.com/vendor/find should bring up the find view but instead it treats it as a username
Jeremy
I see a paramater called "constraints" on the route... would this be something to research for a fix?
Jeremy
Yeah, you will want to look at constraints for the route to resolve it or post another question on SO.
Dale Ragan
looking at how i have this setup, i will probably just change the find action to be the index action of vendor... That makes a little more sense
Jeremy
hey... thanks for all your help... loving stackoverflow and all the help. thanks again.
Jeremy