Url problems in MVC C# - url

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

Related

Get url for custom route in asp.net mvc

In my ASP.Net mvc application I have changed the default route registration in startup.cs to
routes.MapRoute(
name: "default",
template: "{customer}/{language=fi-FI}/{controller=Home}/{action=Index}/{id?}");
So all routes would contain the customer id and the current language. Custom segment matching takes care of putting the customer and language info in new urls so when the requested url is /customer1/fi-fi/somecontroller/someaction any generated url like this
<a asp-controller="othercontroller" asp-action="otheraction">Some action</a>
would be generated correctly with the customer and language code in it.
The question is, how should I generate the url so I can specify the customer and language code without having to do string concatenation? I would need this e.g. in the links for changing language.
I have tried
<a asp-controller="somecontroller" asp-action="someaction" asp-route="default" asp-route-customer="customer2" asp-route-language="en-us">Some action in other language and other customer</a>
but that says that I cannot specify action and controller when asp-route is defined and if I remote asp-route then nothing changes.
I got this working by using #Url.RouteUrl, e.g.
#Url.RouteUrl("default", new { customer = "someothercustomer", language = "someotherlanguage", controller = "somecontroller", action = "someaction" })

Is there a simple way to map a single URL to another URL in ASP.NET MVC?

I have a working ASP.NET MVC application to which I would like to add a very simple URL which I would like to be redirected to another URL, without the Controller part of the path in it.
I.e. I want to be able to tell people to go to mySite.org/Hello and have it direct them to mySite.org/Whatever/WhateverElse?myfun=9, or whatever.
So I added a method in PublicController that redirects Hello to whatever, which works, except it requires me to tell people to go to mySite.org/PUBLIC/Hello, and we'd like to not have the extra word "public" in there.
Hours of confusing study later, I see that I could perhaps add a line in Global.asax.cs such as: routes.MapRoute(name: "Hello", url: "Public/Whatever"); ... or something... but that actually changes the way everything gets routed, and after trying various syntactical variations, nothing has seemed to just change one basic address to map to another basic address.
Is there a simple way to get mySite.org/Hello to map to another URL, without the virtual subdirectory for the public controller?
Create a custom route (it would need to be the first route in the table)
routes.MapRoute(
name: "Hello",
url: "Hello",
defaults: new { controller = "Whatever", action = "WhateverElse" }
);
then /Hello will redirect to /Whatever/WhateverElse

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!

How to make routelink return the correct URL?

here is the procedure to duplicate the issue.
start new mvc 4 project.
add the following lines to the RegisterRoutes() method just before the "Default" route
routes.MapRoute(
name: "Test",
url: "Test/{action}",
defaults: new { controller = "Test", action = "Index" }
);
add the following to Index.cshtml and About.cshtml
#Html.RouteLink("link to TestController", "Test")
As you can see that the generated html code is different for the 2 pages. I would expect the URL is "/Test" which is rendered correctly for Index.cshtml, but it become "/Test/About" for About.cshtml.
My question is if there is any documented way to generate the same correct URL. The above example is created just for demonstration purpose. I would like to use the routing table information to generate menu.
It seems the engine is setting the action route value from the current page ("Index", "About"). So one way to fix this is to "unset" that route value:
#Html.RouteLink("link to Test Controller", "Test", new { action = String.Empty })
Register your route
routes.MapRoute(
name: "TestUrl",
url: "Test",
defaults: new { controller = "TestController", action = "TestAction" }
);
add the following to TestAction.cshtml
#Html.RouteLink("Test", "TestUrl")
Now your URL is /Test, hoping this will help you.
Quickest way would be to just specify a blank action.
#Html.RouteLink("link to TestController", "", "Test")
This will produce /Test
For those who arrive here looking for .netcore razor page RouteLink syntax, don't use RouteLink's "page" reserved member. It will not work for the routeValues object as some have suggested. Instead, use the following format:
<a asp-page="RazorPage">RazorPage</a>
This handles trailing and the absence of trailing forward slashes. The RazorPage is the name of the page without any characters after the first ".".

ASP.net MVC Route 404 with Encrypted text

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

Resources