For example, when I visit http://www.nerddinner.com/Home/About/, the About tab points to http://www.nerddinner.com/Home/About/ which is what I expect.
However, If i were to visit http://www.nerddinner.com/Home/About/WhyDoesThisLinkChange, not only is this page valid but the About tab also points to http://www.nerddinner.com/Home/About/WhyDoesThisLinkChange. Why does ASP.NET MVC do this and how do i prevent it from doing it?
Can you show your routing setup? Something is wrong with it I'd suggest. The second example URL shouldn't map to the same action.
You probably have a parameter in the routing - and correctly MVC is preserving this parameter.
In principal if you have
/product/view/17
Controller = product
Action = view
{productId} = 17
So in this case it makes sense to preserve the Id of the product across requests to the same action.
But in your case you don't really want both those URLs to map to the same place. That said once you are there - with the second URL - it makes sense for MVC to use the same URL to get back to the same action.
UPDATE: If you want to explicitly stop a parameter being accepted for this URL then you need to make the parameter accepting routing option more specific so it excludes this URL, or put in a routing option above it without a parameter than accepts only the Home/About URL.
Related
I have a website built from Ember.js. A user can access a page through URL http://..../view?showTitle=true. However I don't want to explicitly expose the parameter showTitle=true to the user (meaning user will only see http://..../view). This URL is automatically generated and serves as a redirect destination URL. So, I have to remove it manually somewhere before the page load. But I still need this value of this query parameter to query data. Is there a way to achieve that (A example would be great)? What about do it without refreshing the router?
Actually, an example of your scenario would be greater :)
There are some other ways to store data while transitioning to a route. Such as: storing the params in transition object or storing the value in a service. Create a redirection route, use beforeModel hook to grab the value from query params then do a redirection to the real route.
A working example is: ember-twiddle-1
By the way, even if you don't describe your queryParamsin your routes, you can access them via transition.queryParams. This seems a bit hacky. But it works: ember-twiddle-2 (Note: It doesn't work in the same route.)
Updated:
At the setupController hook you can override the controller parameters. So you can remove the parameters from the url. ember-twiddle-3
Am having many xhtml pages in my application. First page that a User gets to see is named index.xhtml, when User is asked to update his Profile it will be updateProfile.xhtml etc. When I hit my application my page names gets displayed on the URL. When user is updating profile, URL will be http:/myDomain/myServlet/updateProfile.jsf.
Am interested in knowing whether its possible to map all my xhtml page names which gets displayed on the URL to some other name. For eg., in the above case, I want all the URLs which matches *.jsf pattern to be displayed to user as http:/myDomain/myServlet/myAccount.
I dont see a possible threat if a end user gets to know my page names, but still, I dont have much knowledge on Security/Hacking, so atleast I dont want to display *.jsf in my URL. Because, a user can know that am using JSF.
In JavaServer Faces to rewrite URL you need basically a Filter. In your case your rewrite could be done by something like PrettyFaces http://ocpsoft.org/prettyfaces/.
With PrettyFaces you will be able to configure rewrite patterns and everything you need.
Edit : When creating your own Filter to redirect URL, you also need to create a ViewHandler and override the getActionURL() function so that actions will go to the new URL.
I am in the process of generating a URL dynamically in my cshtml page.
What is the difference between Url.RouteUrl() & Url.Action()?
Which one should I use to generate the URL & what difference do both have in terms of implementation ?
Thanks in advance.
RouteUrl generated the url based on route name. If you have multiple routes with similar parameters the Action method may pick a wrong one - it works based on the order of route definitions. This may take place when your routes have optional parameters.
If you want to make sure that a certain route url will be used you need to call RouteUrl passing this route name. Route names are unique and clearly identifies a route.
One more difference is that Action is MVC specific (it uses controller and action names), while RouteUrl is generic is and can be used without MVC (you can have routing in WebForms).
Url.RouteUrl allows you to specify a particular route by name. This will force the usage of that route. Url.Action will simply pick the first route that matches the criteria.
I'm using AnchorLink on a very simple site with just two routes defined, one standard route and another area route for admin/{controller}/{action}/{id}. I'm at a URL like:
/admin/release/push/255
In that view I'm using:
#Html.AnchorLink("Confirm", "Confirm")
AnchorLink is rendering a link without the current request {id} included, ie. /admin/release/confirm! If I inspect the RouteValues I can see {id} is in there. If I explicity pass in the route values from teh current request, as in:
#Html.AnchorLink("Confirm this release", "Confirm", Url.RequestContext.RouteData.Values)
Then it works, I get the URL /admin/release/confirm/255. Why does the explicit version where I pass in the current request route values work, but the implicit one, without the route values argument, which I thought would pick up the current request route values, doesn't? I know the above is a solution, but it's ugly and there's some underlying reason why the AnchorLink without the route values isn't working as I expect?!
MVC is doing exactly the right thing here. It's not to know you require the Id parameter for your anchor link or not -- in other words its not trying anything clever to pre-parse and examine the link. It will only do that when its being rendered. And in your case without the id parameter specified somewhere its going to use the default route.
If you find yourself writing that same code all over the place you could easily extract it out into a static helper class. That can get rid of the ugliness.
To support legacy URLs in my application, I use a regex to convert URLs of the form /Repo/{ixRepo}/{sSlug}/{sAction} to the new form /Repo/{sName}/{sAction}, using the ixRepo to get the correct sName. This works well, and I can redirect the user to the new URL with a RedirectResult.
However, I'd like to catch legacy URLs with an invalid action before I redirect the user. How can I verify if a URL string will map to a registered route? MVC clearly does this internally to map a request to the correct action, but I'd like to do it by hand.
So far, I've come up with this:
var rd = Url.RouteCollection.GetRouteData(new HttpContextWrapper(new HttpContext(
new HttpRequest("", newPath, ""),
new HttpResponse(null))));
which appears to always return a System.Web.Routing.RouteData, even for bad routes. I can't find a way to check if the route was accepted as a catch all, or if actually mapping to a route that's registered on the controller.
How can I use MVC's routing system to check if a URL maps to a valid controller/action via a registered route?
(I've seen ASP.NET MVC - Verify the Existence of a Route, but that's really inelegant. MVC has a routing system built in, and I'd like to use that.)
Wrong question. Anything can be a route, whether or not it actually maps to an action.
I think you're asking, "Will this execute OK, or will it 404?" That's a different question.
For that, you need to do what MVC does. Look in the MVC source at MvcHandler.ProcessRequestInit and then ControllerActionInvoker.InvokeAction to see how MVC looks up the controller and action, respectively.
If you know the controller and ask for valid actions, just do some reflection stuff as done in here.
If the redirected url goes to your application, then you can check if the url goes to a valid route. Some code on haacked.com http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx does route testing as a unit test. After this you have controller and action as routedata and you have to do, what Craig said "do the same as mvc does".
The routing system maps request uris to route handler. The mvc route handler (class) throws an exception if it fails. There is no checking.
You can add constraints to your routes. If you constrain the action property. Then checking if the url goes to a valid route my be what you want.