I'm using MVC 4 and am customizing my routes.
I have a Partners action in my About. By default MVC matches the route /About/Partners to this action. I have set up a custom route so that /Partners maps to this action. My question is how do I make it so that /About/Partners no longer works for that action. I don't think I can alter the default route in the route.config file because it is being used by other actions.
Try putting the new route BEFORE the default route
Related
I'm new to razor pages (used to MVC pattern). I came across some interesting routing pattern I saw on GitHub:
services.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Edit", "/{Slug}/Edit");
options.Conventions.AddPageRoute("/Delete", "{Slug}/Delete");
options.Conventions.AddPageRoute("/Details", "{Slug?}");
options.Conventions.AddPageRoute("/Details", #"Index");
options.Conventions.AddPageRoute("/Create", "{Slug?}/Create");
});
In some projects, I don't see the AddRazorPagesOption
Just out of curiosity, what would happen if I didn't specify routing like this?
The method options.Conventions.AddPageRoute just help you to define custom routing for your pages. So, the razor page:
Edit will have route template /{Slug}/Edit where Slug is a parameter from the URL.
Delete will have route template /{Slug}/Delete where Slug is a parameter from the URL.
Details will have route template /{Slug?} where Slug is a optional parameter from the URL. Also you can go to that page using /Index.
Create will have route template /{Slug?}/Create where Slug is a optional parameter from the URL.
If you remove them all your razor pages will just use the defaut conventions which is folder base convention started the Pages folder as the root folder. For pages that have parameters you should pass them as query parameter in your request URL. For example without the conventions defined you'll have to use /Edit?slug=myValue if you want to go to the Edit page of myValue item. With the convention configured, the URL is pretty because you'll use /myValue/Edit.
I think the configuration you actually have is just here to avoid having parameters of your razor pages to be passed as query parameter in your request URL.
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"}
);
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. :)
I've started using Piranha CMS and really enjoining it, but now I came into a bit of trouble.
One of my pages is a contact form where I want to post an extended page model with contact information to the controller.
I've created a page template Contact and in the manager gui and I've set the setting View to Contact
The correct view is loaded but the problem is that all the requests goes against the index action method and not the contact action method in PageController.
What am I doing wrong?
The view is used to signal the controller what view should be used to render the page, not which action should be called. This can be used when several pages has the same type of data and logic, but should be rendered differently.
The field route is used to rewrite a page to a controller/action. The default route for a page is Page which means that requests to that page is rewritten to ~/page, i.e the PageController and its Index action. If you wanted a certain type to be rewritten to the contact action of the PageController you set the route to Page/Contact which will rewrite the request to ~/page/contact.
If you have a complex structure you should add custom controllers, for examples a ProductController. The route could then be set to Product rewriting the request to ~/product.
I hope this clarifies things!
Regards
HÃ¥kan
I am new Refinerycms. I create my own engine called "foodfind" (FoodFinder\vendor\extensions\foodfinds). to become flexible I want to add more functions in client side controller (app\controllers\refinery\foodfinds\foodfinds_controller.rb) and their view pages.
I am trying to do this but getting an error No route matches {:controller=>"refinery/foodfinds/foodfinds", :action=>"new", :locale=>:en}
help
Use rake routes to check if you have a route for that action. If not, then you have to add it to the routes file.