ASP.NET MVC 4 - Is there a way to shorten URLs? - asp.net-mvc-4

Is there a way using routes in mvc to shorten the url? I'm fairly new to MVC, and I guess I don't quite yet understand what routes.IgnoreRoute()does fully.
So for the sake of example, using routes could I make this,
site.com/webFolder/controller/action
look like,
site.com/controller?
Also if it helps we're currently using IIS7. Thx all.

Yes you can create custom routes. Take a look at this:
http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/creating-custom-routes-cs
In the example given a route of /Archive/{entryDate} was created using
routes.MapRoute(
"Blog", // Route name
"Archive/{entryDate}", // URL with parameters
new { controller = "Archive", action = "Entry" } // Parameter defaults
);

Related

I want to change path in ASP.NET Core 5 MVC

I'm working on an ASP.NET Core 5 MVC project. My client wants a different link format, like:
http://studentfintech.app/short_form
But, at this time, the page link is:
http://studentfintech.app/Money/short_form
If I remove the MoneyController, this will be removed, but this is not accessible without a controller.
Please share your ideas to access this page according to this link:
http://studentfintech.app/short_form
This can be useful for you:
Routing in ASP.NET Core MVC
You can specify a route like this:
endpoints.MapControllerRoute(
name: "short_form",
pattern: "short_form",
defaults: new { controller = "Money", action = "short_form" });

ASP.NET MVC Routing catch all

I'm using ASP.NET Core MVC and using attribute routing in my controllers.
In my Configure method in Startup.cs I currently call app.UseMvc() to start my MVC application.
Everything works as expected.
Now I'm trying to get a catchall going, but the 404 always gets the best of the situation.
I changed my app.UseMvc to the following:
app.UseMvc(routes =>
{
routes.MapRoute("Default",
"{*catchall}",
new { controller = "Index", action = "Index" },
new { catchall = #"^(.*)?$" });
});
but no dice.
I even tried to remove the catchall regular express as well, but I still get forwarded to my 404 page.
Any help is appreciated!
For anyone having the same problem, Tom Droste pushed me into the right direction
Adding a conventional route and using attribute routing has subtle side effects
The catch-all route was added first in the route dictionary and the attribute routes are added afterwards.
The AttributeRouting class just creates an instance of AttributeRoute which is an IRouter responsible for building the attribute routes. This is done on the first time AttributeRoute is invoked
Having that, the catchall was thus never called because it's not the last route.
putting everything back into conventional routing fixed the problem.
I suggest reading https://luisfsgoncalves.wordpress.com/2015/08/18/asp-net-5-routing-part-ii/ to understand how attribute routing works
Have you tried this?
routes.MapRoute(
"Default",
"{*.}",
new { controller = "Home", action = "Index" }
);
In your Startup.cs file, place this line of code just above app.UseMvc() in the Configure() Method. Obviously you can redirect to whatever page you want.
app.UseStatusCodePagesWithRedirects("~/Home/Index");
EDIT:
Per you comment, as far as I know, you'd have to write your own middleware to redirect and keep the original url/query parameters. If you're not familiar with developing middleware, I posted a demo project on GitHub that demonstrates how to develop middleware in three easy steps. You can download the project here.
So if I understand you correctly you just want a route that will be the one when the rest isn't matched. In my knowledge there are atm two ways to fix this. The correct one would be to write some routing middleware, however the dirty solution is to create a route with all nullable arguments that will be matched as last.
routes.MapRoute(
"DirtyRoutingSolution",
"{notUsed?}/{notUsedTwo?}/{notUsedThree?}/{notUsedFour?}",
new { controller = "Home", action = "Index"}
);

ASP.NET MVC intercepting route handling

So I am facing this little problem. In my route config I have configured two routes. One default route, which handles controller/action request and one custom route which is basically website.com/my_route where my_route is received by some controller's action as an argument. the problem is that whenever I want to give that action a parameter which contains a '/' in it, the route handler resolves request to controller/action route config, so if looks like this website.com/my_route/my_subroute, you get what happens. the my_routeController is searched and of course it does not exist, now the number of '/' in url can be more than one as you see and I want to do such thing. if the appropriate controller with appropriate action does not exist I want this request to be handled but that one controller which's action will recieve my_route/my_subroute as an argument. What is the best practice in this situation. If need I can provide the route config.
Try following way in your route.config
routes.MapRoute(
"my_route",
"my_subroute/x/y/z",
new { controller = "controllername", action = "actionname" }
);
Let me know if this fixed your issue. :)

Asp.NET MVC 4 routing a specific URL to a certain controller

I want to run a certain Asp.NET MVC 4 Controller when an URL -probably irrelevant with the controller name- is entered. For example, if the user opens the address "localhost:3364/abc/def", I want to run the controller with the name, for example, "SugarController". Is it possible, or do I have to start my URL with the word "Sugar"? I know that URL routing can be done by adding some code to Global.asax file in the project. But I don't exactly know how to manage this one.
Thanks in advance.
This should work
routes.MapRoute("Fixed", "abc/def", new { controller = "Sugar", action = "def"});

MVC4 extra folders under View

Im currently implementing MVC into a fairly large project and want to sort views into categories. MVC dont seem to understand this and Im having problems finding a clean solution. Basically I was hoping to solve this with routes, but it isnt working.
My folder structure in the project is like this:
- Controller
- SLResources
- FAQController.cs
...
- View
- SLResources
- FAQ
- (cshtml files in here)
Ive also tried adding a - FAQ folder after - SLResources in the controller folder structure.
Ive then made the following routing, with no luck:
RouteTable.Routes.MapRoute(
name: "FAQ",
url: "SLResources/FAQ/{action}/{id}",
defaults: new { controller = "FAQ", action = "Index", id = UrlParameter.Optional }
);
basically I would like to reach the FAQ by using this url: http://www.xxxxxxxx.com/SLResources/FAQ/
Is the only solutions to either create a dummy class that redirects to the proper views, or a custom ViewEngine?
Any tips?
If your goal is to have the url with SLResources/FAQ, then you don't need to create a separate folder in the Views.
Your route already does that for you. Leave your route as is, and put your cshtml files in the 'Views>FAQ' folder and it will work.