ASP.net MVC Route 404 with Encrypted text - asp.net-mvc

I'm implementing a Password Reset facility in an asp.net MVC 3 web application. The email sent to the user contains a link with an encrypted string. Below is a sample link:
forgotprocess/QU1jfNoTb1Qd7qObop1FinQai4hCbzg7%2bMGfMF63d9Vvahi%2bmg9cT8KyaGo9jE1gbsWl5r%2f6DzpcRLf6HYNGeeFujG9QeblKUUvfxLDJ7UwcSCKD2AdsrR3EmC80PNCGGnGMQiya7ILNOJjWh%2fKSRQ%3d%3d
When the link is clicked I get a 404 error. To test the routes I used RouteDebugger, however I still get the 404 page - seems that the link isn't getting to the application. However if I change the link to the following:
forgotprocess/?i=QU1jfNoTb1Qd7qObop1FinQai4hCbzg7%2bMGfMF63d9Vvahi%2bmg9cT8KyaGo9jE1gbsWl5r%2f6DzpcRLf6HYNGeeFujG9QeblKUUvfxLDJ7UwcSCKD2AdsrR3EmC80PNCGGnGMQiya7ILNOJjWh%2fKSRQ%3d%3d
It works fine. I'd prefer not to have to use a query string parameter.
The size of the overall link is about 200 characters, so it shouldn't hit any limits?
Mark

In your top route:
forgotprocess/QU1jfNoTb1Qd7qObop1FinQai4hCbzg7%2bMGfMF63d9Vvahi%2bmg9cT8KyaGo9jE1gbsWl5r%2f6DzpcRLf6HYNGeeFujG9QeblKUUvfxLDJ7UwcSCKD2AdsrR3EmC80PNCGGnGMQiya7ILNOJjWh%2fKSRQ%3d%3d
%2f is going to get URL decoded into / so it's going to confuse the routing engine.
Can you alter how you're encrypted string is being generated to prevent this?
Alternatively, if it's the last parameter, you could alter your route like in this post, but that might lead to other issues:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{*id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }); // Parameter defaults

Related

Url problems in MVC C#

I have a problem in MVC project in url.
In the Route config I have the following code:
routes.MapRoute(
name: "Test",
url: "{controller}/{action}/{id}/{selected}/{category}/{engineId}‌​",
defaults: new { controller = "Product", action = "SubCategories"}
);
The parameter category contains the name of selected category. In database I have the category with name: "Packet / Set". If in my website I choose this category and in the url will appear this categoryname
SubCategories/92/Bertone-FREECLIMBER-2.0/Packet / Set /33720‌%E2%80%8B.
I got the
Server Error in '/' Application.
The resource cannot be found.
error.
If the the category name doesn't contain the "/" character, evrything works fine:
SubCategories/94/Bertone-FREECLIMBER-2.0/Air%20filter/33720%E2%80%8C%E2%80%8B
Could you advise how should I resolve this?
You need to encode the all slashes in URL as %2F. You can use Javascript encodeURIComponent() function. MVC automatically decodes URL and action parameters so you should be able to get values correctly in your controller without any additional effort.
Take a look here for more info: https://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

Changing URL without changing Actual Path to Redirect

I am new to ASP.Net and working on MVC 4. I want to replace my current URL with a customized URL.
For example:
Current URL: http://www.testsite.com/home?pageId=1002
Desired URL: http://www.testsite.com/1002/home/
So the URL that is displayed in the address bar will be the desired one and actual URL working will be the current one.
I have tried URL routing in Global.asax file of my project but doesn't seems to be working for me.
What exactly I want is to put the URL Like this.
Thanks in Advance.
ASP.NET MVC 4 provide a toolbox way to write your application. The URL that you see in the browser comes from Routing that do the hard work to convert url to app routes and app routes to url.
1) The default ASP.NET MVC 4 Template project comes with a file at App_Start folder named RouteConfig, where you must config the routes for the app.
2) The routes has precedence order, so, put this route before the default one:
routes.MapRoute(
name: "RouteForPageId",
url: "{pageId}/{action}",
//controller = "Home" and action = "Index" are the default value,
//change for the Controller and action that you have
//pageId is the parameter from the action that will return the page
defaults: new { controller = "Home", action = "Index" }
);
Now you can enter myappdomain/1220/index for exemple.
Hopes this help you! Take a look here for more info ASP.NET Routing!

MVC2: Need to route Urls containing # symbol

I am looking for a way to correctly route urls that contain the '#' symbol. For such urls i basically want to ignore the #.
For example I want Stores/Index/#/{storeName} to route to the Index action of the Store controller passing a single parameter (storeName).
I have tried matching the literal '#' in the string but this is not working. (The action is called but the storeName parameter is not passed)
routes.MapRoute("RemoveHash", "Store/Index/#/{storeName}",
new {controller = "Store", action = "Index", storeName = UrlParameter.Optional});
I have also tried having 2 parameters to the action (the first being the #), thinking that I could just ignore the # if it was passed (hacky i know)... but something goes wrong with the routing in this case and neither parameter is passed to the action.
I would like to avoid using a HttpHandler for this task, if I could handle this using the MVC routing system that would be ideal.
Any suggestions?
What you are trying to do is impossible. Everything that follows the # (hash) sign in the URL is completely ignored when the browser sends a request to the server, so your ASP.NET MVC application could never get this value. Only client side javascript could read this value (using window.location.hash) and could pass it to the server using AJAX request and a normal URL (for example: Store/Index/{storeName}).

Running ASP.NET MVC in a subdomain makes Html.ActionLink render broken links

I am running MVC in a subdomain
http://test.domain.com which points to the /Test directory on my webhost4life account.
Html.ActionLink("About", "About", "Home")
it renders a link to
http://test.domain.com/Test/Home/About -- which gives a 404
the link should be ..
http://test.domain.com/Home/About
is there a way to override ActionLink to omit the /Test on render?
Thank you
Experiment 1
I added a route to the table like this...
routes.MapRoute(
"Test", // Route name
"Test/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
and now action link renders links like this..
http://test.domain.com/Test/Test/Home/About/
when this is clicked it does not give a 404 but gives the Home controler About action.
Result
No more broken links but the site renders ugly urls.
For a site using lots of subdomains I use a nifty MVC extension from ITCloud called UrlRouteAttribute. It allows you to assign a route to every action as an attribute setting the path and name. I have extended this to allow fully qualified paths - so to include the domain/subdomain the controller should attach to. If this is something you'd be interested in I'll upload a copy somewhere.

Strange route problem in ASP.NET MVC - default route not hit

I have this two routes currently in my application after decommenting out many other ones. Let me first explain that I have quite a big application already but have come to a problem where my application does not start at the root url anymore.
If I set starting page to default.aspx then webapp starts at (example) http://localhost:55421/Default.aspx. I don't want that. I want it without Default.aspx
So I went into app properties and removed Default.aspx as starting page - now it is blank field (just like in a sample new MVC app if you create it in VS 2008).
But now application does start at the required URL but issues an error:
"The incoming request does not match any route."
Also If I use route debugger it also misses all routes and catches it by catchall route.
I don't know how all of this is possible since as I said above I have two default routes configured at this time:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pages", action = "Display", slug = "Default" }
);
Any help appreciated
Am I right in thinking you are trying to hit
http://server/{controller}/{action}/{id}
with
http://server/
If you are I think you need to provide a default for the last parameter {id}. You have a default for a parameter slug but without a default for {id} I don't think ASP.NET Routing can hit it.
If I'm right
http://server/Pages/Display
should also not hit the default route, because you are expecting id in Display?
HTH
Alex

Resources