I have a controller named ProfileController, and what I'm trying to do is make it so I can redirect or search for users based on their usernames. Something like www.site.com/Profile/MyUsername and then it bring up a view with that particular user model. Only way I can come seem to do this is to pass an int Id as a parameter but even then it comes out as www.site.com/Profile/Index/1 when I'd really like to get rid of the Index part altogether and replace the 1 with the actual username... Has anyone done this before?
Related
I want to achieve what stackoverflow does with url.
domain.com/questions/1234/properTitle <-- fine!
domain.com/questions/1234/wrongTitle <-- redirect to domain.com/questions/1234/properTitle
But I don't want to contact the database twice.
For example, inside the question controller action, I am already querying all the info based on 1234 question Id. I do not want to redirect and lose all this information and then query again. Should I put it in TempData or something? What efficient way to handle this?
I think it's more complicated (and maybe even less efficient) to manually cache and retrieve the data in your application. Your database server probably caches the result anyway and the second query won't take very long.
Update: You can redirect to an overloaded action with the model as an argument. This could look somewhat like this:
return RedirectToAction("Action",
new {
id = 1234,
title = "properTitle",
model = myAlreadyRetrievedModel
});
I have a controller that uses the following structure:
.com/Object/375
However, I can also use the following URL when I am accessing special admin rights
.com/Admin/Object/375
I use the same user controls whether you're in the Admin section or not, but they both point to the same Controller Object. I need for the links to maintain that URL structure and not try to kick an Admin user back to the Object controller. I am currently using the route name method, where these are my route names (in global.asax):
"Admin/-Object"
"Object/-Object"
"Object-Object"
These route names catch the following routes:
Admin/Object, Admin/Object/555, Object, Object/323
I then use the following in a route link
Html.RouteLink(id, Request.Url.Segments[1] + "-Object", new { id = id })
This works just fine, but has an odd smell - any other ideas?
To clarify: I need the URL to be properly created based on the current URL structure (with or without the Admin) and the routing will point to the correct controller (the same for both URLs) and the admin specific content will be injected into the page only if in the Admin section (based on URL).
Just to wrap this up, using ViewBag is probably a better idea because using the URL segment might result in unexpected errors, especialy if you move the controls or views around.
This may be more of a best practice question.
I have three views (create/details/edit) that all link to their own results view (createResults/detailsResults/editResults). Each results view shares a partial with a results table on it.
When a user submits one of the three (c/d/e) views, should each results view have its own action, even tho the action will quite literally do the exact same thing (search on the information on the c/d/e view)? I'd rather not duplicate this code if not necessary.
Should I have one action, and pass in something that tells the action which results view to direct to? Is there an easy way to get the referring view in the action?
If you have 3 actions you don't need to duplicate code. Why not refactor the common code into a single private method on the controller, or perhaps even move it into an action filter?
I would make a single action with a string parameter containing the view name.
You can play with the routing table to make the URLs prettier.
My url http://server/region/section/item
Now if someone goes to http://server/us/ I want to display a page to choose a section
if someone goes to http://server/us/beer/ I want to display a list of all beers.
My question is should I just have 1 route with defaults and then return different views depending on how much the URL is filled in or should I create multiple routes going to the same controller different actions? or even different controllers, I just want to know what the best practice is.
The typical route looks like this:
http://www.domain.com/controller/action/id
[domain/controller/action/id]
In your case, it's short one part:
http://server/us/beer
[domain/controller/action?/?]
As Robert Harvey said, you wouldn't want to have an action for every product. So how about something like:
http://server/us/product/beer
[domain/controller/action/id]
domain = server
controller = us (tho, this doesn't seem like it would be a good name for the controller)
action = product
id = beer
Then you'd develop a product view that would show the beer data to your visitors.
This isn't architect-ed very well, but without knowing your situation it would be difficult for anyone to answer this. I hope this helps though.
In your particular case, "beer" can probably be a filter to a controller action, rather than another controller action, in which case you need only one route entry.
It doesn't seem wise to create a strategy that will require you to add new routes, controller methods and/or views every time you add a product category. Unless, of course, you want to highly customize each category's look and behavior.
I'm guessing the StackOverflow code has something along the lines of a UsersController that defines a function like this:
public ActionResult Profile(string id, string username, string sort)
{
}
From what I can tell, there's two ways to go about implementing the Profile function. One is to use a switch statement on the sort parameter and render a different view based on what is being displayed (e.g. stats, recent, responses). These views would then render a partial user control to handle the display of the top half of the profile page (gravatar, username, last seen, etc).
The other way I could see implementing this would be to always render one view and have the logic for showing / hiding its different sections based on the sort. This would lead to a pretty monstrous view page, but it should work as well.
Are there any other ways of implementing the StackOverflow profile page that I'm missing? The reason I ask is because my current ASP.NET MVC page has a similar profile page and I want to make sure I'm not going about this the wrong way.
Personally, I would create an action and view for each tab section and use a partial view for the top part that is shared across the others. I'm just getting started with MVC though, so I don't have a lot of experience to back up that suggestion.
The URL route scheme I would use is /{controller}/{id}/{section} e.g. /users/123/recent /users/123/responses, etc.
You could build the view name from the sort value
<% RenderPartial(sort + "View") %>
However, it does default back to the stats view if the parameter doesn't exist so I don't think they are doing that.
A switch on sort would probably work just fine with the default on the switch going back to the stats view.