I have 3 routes total in my code.
If I change the position of 1st with 2nd, then 2nd starts to work but 1st gives 404 error.
Am I doing anything wrong in below code?
This is GET route and works perfectly.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new {
controller = "Category",
action = "Index",
Category = UrlParameter.Optional
}
);
This is GET route and always give 404 error.
routes.MapRoute(
name: "Default1",
url: "{Category}",
defaults: new {
controller = "Product",
action = "Index",
Category = UrlParameter.Optional
}
);
This is POST route and works perfectly.
routes.MapRoute(
name: "Default_Without_Action",
url: "{controller}/{action}/{Category}",
defaults: new {
controller = "Product",
action = "GetProducts",
Category = UrlParameter.Optional
}
);
test if you put url: "{controller}" and see if this works
routes.MapRoute(
name: "Default1",
url: "{controller}",
defaults: new {
controller = "Product",
action = "Index",
Category = UrlParameter.Optional
}
);
I checked both routes and working fine.
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Route -1
routes.MapRoute(
name: "Default1",
url: "{Category}",
defaults: new { controller = "Product", action = "Index", Category = UrlParameter.Optional }
);
//Route - 2
routes.MapRoute(
name: "Default",
//url: "{controller}/{action}/{id}",
//defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
url: "{controller}/{action}/{Category}",
defaults: new { controller = "Category", action = "Index", Category = UrlParameter.Optional }
);
}
On initial/first request it matches the route-1 and calls the Index Action Method of Product Controller as shown in the picture below. Reason - We defined the defaults parameter for controller Products.
If we remove the defaults parameter from route 1 then on initial/first request route-2 URL pattern matches and Index Action method of Category Controller will be called, Reason - We defined the defaults parameter for controller Category.
Browser response with 404 error only when any request doesn't match with the registered URL pattern.
Related
I have the following link
<i class="fa fa-user-md"></i> Inspections
In my route I have the following
routes.MapRoute("Inspections", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });
In Visual Studio when I click the link it works, on my live server I get a white page with the following in bold
xxx - /inspection/ and also a link [To Parent Directory]
All of my other links in the Application is working fine and as expected
Now I have to go and overide the url in the address bar to go xxx/Inspection/Index
The Index method does also have [HttpGet]
Edit 1:
I tried the following, I also tried removing the route
routes.MapRoute("Inspection", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });
Edit 2:
Here is all the routes I have
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Login", "{controller}/{action}/{id}", new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("Employees", "{controller}/{action}/{id}", new { controller = "Employees", action = "Index", id = UrlParameter.Optional });
//Added the following to try to fix issue
routes.MapRoute("Inspection", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });
Edit 3:
in InspectionController.cs I have the following Index method
// GET: Inspection
[HttpGet]
public ActionResult Index()
{
return View(new ActiveInspectionsViewModel());
}
Edit 4:
Here is the blank page that opens (example)
xxx - /Inspection/xxx - /Inspection/ [To Parent Directory]
Edit 5:
I think were getting closer to the issue, I have disabled Directory Browsing now I am getting the 403 error, which is really weird because all my other Action links works (/Employees, /InspectionTemplates, /Clients, /Emails, /CompanyLevels) It is Just /Inspection that is causing the problem, my url is inspection.xxx.co.za can it be the park domain causing the problem?
Look at your route definitions.
routes.MapRoute("Login", "{controller}/{action}/{id}",
new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("Employees", "{controller}/{action}/{id}",
new { controller = "Employees", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("Inspection", "{controller}/{action}/{id}",
new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });
All three definitions you registered has the same url pattern, {controller}/{action}/{id}.
So whenever you request a page, The request url will be matched against the registered route definitions and when it gets the first match, the request will be send to the controller-action specified in the definition.
So when you request Inspection/Index, it is matching to the first definition (Login) and the request will be send to Login/Index action method.
Since you do not have anything other than the custom pattern in all the three routes,you do not need those. Just remove those and keep the default one.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
}
I am using default route as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{cvg}/{AgencyID}/{AgentCode}",
defaults: new { AgentCode = UrlParameter.Optional, controller = "Login", action = "Index", cvg = UrlParameter.Optional, AgencyID = UrlParameter.Optional }
);
I need to set one route with only Agentcode. I have use as below. but failed to get result.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{cvg}/{AgencyID}/{AgentCode}",
defaults: new { AgentCode = UrlParameter.Optional, controller = "Login", action = "Index", cvg = UrlParameter.Optional, AgencyID = UrlParameter.Optional }
);
routes.MapRoute(
name: "Agency",
url: "{controller}/{action}/{AgentCode}",
defaults: new { AgentCode = UrlParameter.Optional, controller = "Login", action = "Index" }
);
}
Am I missing some?
I find my answer self as
"Make sure the Default route is at the BOTTOM of your listed route table. Order matters when it comes to ASP.NET MVC Routing tables.
The correct ordering is your 'most specific' route to your least specific route."
If a website with url "www.site.com", redirects to HomeController's Index action.
I have
www.site.com/area/controller/action/{nick}
I want the url
www.site.com/{nick}
do the same thing
How I do to create a route and where do I have to create it?
In the RouteConfig.cs or in the AreaRegistration.cs?
In your RouteConfig.cs, add following route after all routes.
routes.MapRoute
(
name: "Nick Route",
url: "{nick}",
defaults: new { area = "AreaName",
controller = "controllerName",
action = "Actionname",
nick = UrlParameter.Optional });
I know that this post is old but if someone is interested, he may find the solution here http://www.nikolay-angelov.eu/custom-url-shortener-in-asp-net-mvc-5/
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "URL",
url: "{shortURL}",
defaults: new { controller = "Home", action = "RedirectToLong", shortUrl = UrlParameter.Optional }
);
routes.MapRoute(
name: "Basic",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
Using ASP.NET MVC4 Routing:
If I'd like to setup a Default routing configuration for the main section of the site:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then another routing configuration, something similiar to the following:
routes.MapRoute(
name: "FOO",
url: "FOO/{controller}/{action}/{id}",
defaults: new { controller = "FOO", action = "bar", id = UrlParameter.Optional }
);
Notice the string "FOO/" in the url (just before the /{controller...
For example, I'd like to be able to access the main section of my site using a url like the following
http://dummyurl.com/bar/1
but then access controllers and actions that have identical names if I was to use
http://dummyurl.com/**FOO**/bar/1
routes.MapRoute(
name: "FOO",
url: "FOO/{controller}/{action}/{id}",
defaults: new { controller = "FOO", action = "bar", id = UrlParameter.Optional }
);
That route will result into something that you might not expect, unless you have an mvc area named FOO. That route will only work with http://yourdomain/foo/foo/any_method_in_foo/id
Figured out by going through routing documentation by "the Gu" that I simply needed to put:
routes.MapRoute(
name: "FOO",
url: "FOO/{controller}/{action}/{id}",
defaults: new { controller = "FOO", action = "bar", id = UrlParameter.Optional }
);
Above:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
in my Routes.config and blammo, good to go. I can now separate and access for example an Admin ("FOO") section in my project.
Route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
my controller
public class HomeController : BaseController
{
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
My Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
Finally my request url
http://localhost:1234/Contact/
Error on browser
The resource cannot be found.
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: /Contact/
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.18033
What am I doing wrong?
Solution:
Custom route should take the precedence
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The Framework always tries to match the URL of the request to a route in the order of the Routes added to the RouteCollection
So you should put the custom routes before the default route,
//Custom route
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
//default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can use:
routes.MapRoute(
name: "Default",
url: "{*p}",
defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
);
The asterisk indicates that it's a catch-all route. Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.
You could also add a route constraint to this route which can determine whether the page exists in the database or something.