Asp.Net MVC custom routing interrupt default routing - asp.net

My routes are not working don't know why. When I try attribute routing it works for for that action but breaks default routing for other controller or actions. I only want to change routing for an index action method of a controller CMS I want it like [Route("{countrycode}/{id}")]. but when I do this default route breaks for other index action methods of other controllers.
For example: When I use [Route("{countrycode}/{id}")] on cms controller's index action then route breaks for Home and other controllers index action methods so I have to call the index methods explicitly like /Home/Index instead of /Home otherwise it throw an error. Thanks in advance.

Related

ASP.NET MVC Only use route to access controller action

I have a controller action that I only want users to be able to access by using a friendly route.
So instead of going to "/controller/action"
They should only be able to go to "/controller-action"
This is the route I have setup for this.
routes.MapRoute("ControllerAction",
"controller-action",
new { controller = "Controller", action = "Action" });
Is there any way to keep this route to this controller/action while redirecting requests from /controller/action to /controller-action ?
The reason this is being asked for is to keep our paths "less deep" for seo reasons.

.NET MVC5 Attribute Routing Optional Prefix

Trying to implement an optional routing prefix and running into some issues. Here is my route definition:
[RoutePrefix("{tenant:range(10,12)?}")]
[Route("{action=Index}")]
public class HomeController : Controller {
Routes are:
{tenant}/Index
{tenant}/Search
Loading the site with a tenant works fine, such as:
https://localhost:44300/10/
Using #Url.Action("Search", "Home") generates a correct link to the Search Action.
Without tenant, the site loads:
https://localhost:44300/
But I am unable to load the "non-default" action such as "Search", returns a 404. Also, the Url Helper generates a link such as: https://search
The range condition is temporary. I will have to further develop a custom constraint to actually verify the value.

Convention-based routing in Symfony2

I'm trying to learn learn how routing works in Symfony2, and so far everything I've read has examples like this:
blog:
path: /blog/{page}
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
This routes requests to /blog/123 to the AcmeBlogBundle Blog controller's "index" action, and passes the 123 parameter as the "page" parameter to that controller action. If no page parameter is passed, then the page defaults to 1.
That's all well and good, but what if you want to simply have a convention based routing system that passes things through like this:
/{bundle}/{controller}/{action}
So, for a URL like this:
/acme/blog/index
It would then call AcmeBlogBundle Blog controller's "index" action.
No specific routing configuration is necessary, it simply infers the bundle, controller, and action from the URL. So you can continue adding bundles, controllers, and actions, and you don't need to modify the routing configuration. It just works.
If this isn't possible, can you at least infer the controller and action from the URL? E.g., perhaps you need a route that specifically identifies the bundle, but can we get the controller and action from the URL?
I read through the Symfony "The Book" page about routing, and I couldn't figure out a way to do this.
No way. This was considered as bad practice and so it was removed from symfony.
But you should take a look at the #Route annotation, as it simplifies configuring routes in such a nice way. Directly attached to the action, there is no lack between config and code.

Html.BeginForm in WebApi - routing

I have a Web API project as part of my solution (also containing an MVC4 project) and within the Api project I am trying to post a form to the Values controller Post method (from a view also within the Api project).
Using Html.BeginForm() or Html.BeginForm("Post", "Values") posts to /Values/Post but I need it to go to /api/Values/Post
Any idea which overload or settings I need to post to the correct location?
I can hit all the action methods fine from fiddler (e.g. localhost/api/values).
You would need to use BeginRouteForm as link generation to Web API routes always depends on the route name. Also make sure to supply the route value called httproute as below.
#using (Html.BeginRouteForm("DefaultApi", new { controller="Values", httproute="true" }))
The API controller uses a different route to the default. It's supposed to be consumed from JS (AJAX) rather than a real form post so there's no obvious support for it in HtmlHelpers. Try:
Html.BeginForm("values", "api")
This would trick it into thinking "values" is the action and "api" is the controller. "Post" is inferred from the http method.

Managing routing while extending a controller

I am trying to override a controller that I use in Frontend:
AcmeDemoBundle:DemoController //route is /demo
For the backend, I extended this controller in AdminDemoBundle:
AdminDemoBundle:DemoController //route is /admin/demo
I haven't overriden any methods in the new controller yet, but all the routes from AcmeDemoController go to /admin/demo.
Does anyone know why?
Is there a solution to override frontend controllers for backend purposes?
Basically, by extending the controller, all the routes are redefined with the same name and a different prefix, which means that the routes are being replaced.
There is actually no point at all in extending a controller for the backend, as controllers are meant to be tiny. The logic of the app must be kept in services instead.
Moving the logic to a service allows a better organisation of the code. There is no need to worry about backend/frontend when talking about services. Just, create the logic in a service, and use the methods in either a frontend controller or a backend controller.

Resources