So I have a custom route in my MVC4 web app below:
routes.MapRoute(
name: "SecondarySportsCategoryLanding",
url: "sports/{name}/{id}",
defaults: new { controller = "FrontCategoriesController", action = "Sports", name = UrlParameter.Optional, id = UrlParameter.Optional }
);
I have that placed above my default one as well. What's happening is when I go to /sports I get this error:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /sports
I have the controller specified, and inside that I have an Action titled Sports with 2 optional parameters (defaulted to nulls). Any ideas?
controller = "FrontCategoriesController" should be controller = "FrontCategories"
Because the framework automatically inserts Controller into the value for you.
Related
I'm using ASP.NET MVC 5 and I'm looking to dynamically choose which controller handles a particular request.
For example, the user might request /ControllerName/ActionName/, but I want to first look and see if in my source I have a controller /CustomCodeDirecotry/ControllerName/ActionName. If I have that controller, then use that to process the request. If I don't then go ahead and use the requested controller.
The key in this is that I don't want the user to know that they were handed off to a different url.
In the end the answer to this question was to provide a namespace when mapping routes. Specifically:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcSite.Special.Controllers" });
I found my answer here
I have a contoller named customer and an action method for GetAllCustomers which recieves single string input parameter as FirstName. The issue is that I want the url to be like
MyAPI/Customer/MyFirstName
but the above stated URL is not working for me instead it working as below
MyAPI/Customer?firstname=MyFirstName
How can I make it work like the first url using ApplicationRouting (nuget package)? I have configured the attrbute route for the action method as below.
[GET("Customer/{firstname}")]
public List<Customer> GetAllCustomersFirstname(String firstname)
{
//code goes here
}
Edit
Here is the reference whatI am trying to achieve but it is not happening for me in ApiController
Attribute Routing Github
Open up ApiConfig in the AppStart folder. You need to add the route for that particular action. I think you'll need more than just what you showed there for route since you have to tell it which action to call and what methods to allow. E.g.
config.Routes.MapHttpRoute(
name: "yourRoute",
routeTemplate: "api/{controller}/{firstname}",
defaults: new { controller = "Customer", action = "GetAllCustomersFirstname" });
Our asp.net mvc 4 application has a route defined as following
routes.MapCustomRoute(
name: "Startup",
url: "{area1}/{param1}/{param2}/{param3}",
defaults: new { controller = "Startup", action = "Index", param3 = UrlParameter.Optional },
namespaces: new[] { "namespaceofmaincontrollers" }
);
We also have an area with the name "area1". We are getting http 404 error browsing to the route.
We have tried using routes.RouteExistingFiles = true; to no avail.
We want to hit the controller action Index of the StartupController defined in the main portion of our application .
Any help will be greatly appreciated.
We figured out that while area registration the namespaces were incorrect which was the cause of the problem. Once corrected our 404 errors disappeared.
I have just published an MVC application. It has a Home controller with a method Create.
The URL is http://www.myurl.com
The name of the virtual directory in IIS is myurl.
When I submit the form that posts to Home/Create I'm getting an error because it's trying to send the data to:
http://www.myurl.com/myurl/Home/Create
rather than:
http://www.myurl.com/Home/Create
I tried adding this route:
routes.MapRoute(
"test",
"Home/CreateNewPlayer",
new { controller = "Home", action = "CreateNewPlayer" }
);
but that hasn't made any difference. Can someone please help me out?
You should not host your application in a virtual directory but rather directly inside the website which has a binding to the www.myurl.com domain.
I am very familiar with URL routing in Web Forms and have a basic understanding of MVC routing but have ran into a scenario I am not sure how to wire up. I want to map the a URLs that do not contain the {controller} in the URL map to a controller.
I want to map the following URLS to the controller "SectionHomePageController"
Books/{Action}
Cinema{Action}
Collections/{Action}
Games/{Action}
I know the typical way of a route would be {controller}/{action} so I am not really sure how to wire up these URLs to a controller. So how do I wire up these URLs?
Map a route (should be above your regular {controller}/{action}/{id} route so it takes precedence) that has a constraint and defaults to your SectionHomePageController:
routes.MapRoute(
name: "SectionHomePage",
url: "{prefix}/{action}",
constraints: new { prefix = #"(Books|Cinema|Collections|Games)" },
defaults: new { controller = "SectionHomePageController", action = "Index" }
);
Note that you now won't be-able to have a BooksController, CinemaController etc as this route will override the default one.
routes.MapRoute(
"SomeRoute", // Route name
"Collections/SomeAction", // URL with parameters
new { controller = "SectionHomePageController", action = "SomeActionOnController", id = "" } // Parameter defaults
);