MVC3 not finding resources - c#

I'm trying to start a mvc3 website. But when I load the server 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.
My routes file looks like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"States", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "States", action = "Index", id = "" } // Parameter defaults
);
}

I noted that you have two possibly conflicting route registrations. Try removing the first registration and leaving only this one:
routes.MapRoute(
"States",
"{controller}/{action}/{id}",
new { controller = "States", action = "Index", id = "" }
);

Related

c# Route config for specific URL only

I have this URL
http://localhost:51095/Person/Walk/10
Using RouteConfig how can I remove the controller part without affecting other URL's.
So this will become
http://localhost:51095/Walk/10
This is the order also, they are affecting other URL's
routes.MapRoute(
"Walk", // Route name
"{action}/{distance}", // URL with parameters
new { controller = "Person", action = "Walk" } // Parameter defaults,
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
But this one also affected other Routes
You'll have to be more specific:
routes.MapRoute("Walk",
"Walk/{distance}",
new { controller = "Person", action = "Walk" });

ASP.NET MVC service route overrides default route

I have added WCF service to MVC 5 application, and created a route for it:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new ServiceRoute("Service1.svc", new ServiceHostFactory(), typeof(Service1)));
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The problem is that all my links leads to the Service1.svc route now. #Html.ActionLink("Passport Maker", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" }) become http://localhost:50099/Service1.svc?action=Index&controller=Home and other links change in the same way.
If I add ServiceRoute after "Default" route, the links work correctly but the service is unavailable.
Why it happens (there is no "Service1" in the links, why they select the service route then?) and how to fix it?
The solution:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = "^(?!Service1.svc).*" }
);
routes.Add(new ServiceRoute("Service1.svc", new ServiceHostFactory(), typeof(Service1)));
Explanations for those who may encounter a similar problem: the reason of the problem was that Html.ActionLink uses the first matching route to generate the link. And my service route was the first and was matching, because a route does not require to include {controller} and {action} parameters to be matched (as I thought initially).
The solution is to put the default route first, so it is used by Html.ActionLink. And to still be able to use the service route, need to exclude it from the the first route using constraints. The regex ^(?!Service1.svc).* matches only those controller names that don't start from "Service1.svc".

Getting HTTP Error 403.14 - Forbidden when accessing MVC application with default route

I have this in my route config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "Accessibility/{controller}/{action}/{id}",
defaults: new { controller = "Cardholders", action = "Index", id = UrlParameter.Optional }
);
}
However, when I view my project in browser, it doesn't seem to redirect to the action that I have specified. It remains at http://localhost:54358/ which is probably why I'm getting the http error.
I have no problems viewing the page directly, e.g., browinsg it at http://localhost:54358/Accessibility/Cardholders/Index
What could be the issue here?
You don't have a route that match localhost
If you try "localhost:54358/Accessibility" it should work
If you want that localhost goes to the cardholders page, your route should be:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Cardholders", action = "Index", id = UrlParameter.Optional }
);

How do I map the root directory to a controller in c# using web api?

I have a c# web api (.net 4) web application. I have several controllers. I'd like to map a new controller to the root directory for the web app.
So, I have http://localhost/listproducts
I'd like the url to be http://localhost
But I don't know how to tell the controller to use the root directory. It seems to be configured based on the name.
The solution I went with is:
config.Routes.MapHttpRoute(
name: "ListProductsApi",
routeTemplate: "",
defaults: new { controller = "ListProducts" } // Parameter defaults
);
The trick is the defaults: new { controller = "ListProducts" } line. (I need a POST action so I just left action out. Apparently when you want the root route "/" you need to explicitly name the controller.
You simply need to change the default route you already have. It should currently look something like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
Change controller = "Home" to controller = "listproducts".
It should be fairly easy, just change a default route in your global.asax.cs and instead of
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
change it to
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "ListProducts", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

How to fix a 404 with routes in ASP.NET MVC?

I'm having a problem trying to get routing to work with ASP.NET MVC 3.0. I have the following routes declared:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional }
);
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional }
);
routes.MapRoute(
"TestRoute2",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
When I visit:
http://localhost
The site works correctly, and it appears to hit Default route.
When I visit:
http://localhost/1
I get a 404:
Server Error in '/' Application.
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: /1
Here are the actions those routes correspond to:
public ActionResult Index3(int? id)
{
Product myProduct = new Product
{
ProductID = 1,
Name = "Product 1 - Index 3",
Description = "A boat for one person",
Category = "Watersports",
Price = 275M
};
Product myProduct2 = new Product
{
ProductID = 2,
Name = "Product 2 - Index 3",
Description = "A boat for one person",
Category = "Watersports",
Price = 275M
};
ViewBag.ProcessingTime = DateTime.Now.ToShortTimeString();
if (id == 1)
return View("index", myProduct);
else
return View("index", myProduct2);
}
How do I structure my routes so that all three action methods are hit correctly?
ASP.NET MVC Routing evaluates routes from top to bottom. So if two routes match, the first one it hits (the one closer to the 'top' of the RegisterRoutes method) will take precedence over the subsequent one.
With that in mind, you need to do two things to fix your problem:
Your default route should be at the bottom.
Your routes need to have constraints on them if they contain the same number of segments:
What's the difference between:
example.com/1
and
example.com/index
To the parser, they contain the same number of segments, and there's no differentiator, so it's going to hit the first route in the list that matches.
To fix that, you should make sure the routes that use ProductIds take constraints:
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
new { id = #"\d+" } //one or more digits only, no alphabetical characters
);
There are other issues with your set up, but those are two things that come to mind right off the bat.
Your routes MapRoute Default should be the last.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional }
);
Push the most generic route to the last of the MapRoute call chain.
Try this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"TestRoute",
"{id}",
new { controller = "Product", action = "Index3", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } // Parameter defaults
//new { controller = "Product", action = "Index2", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"TestRoute2",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Move your Default route to the end, Default route should be the last route to define because it acts as a catch all route
In my case, the answer for the same problem was a matter of needing to "include in project" the relevant controllers and views instead of incorrect routing rules.
When mine were created, they weren't automatically included for some reason. This problem was revealed after I closed and re-opened the solution.
{+1 hate} awarded to Visual Studio for its faulty hyper-automation sending me digging through Web.Config files, trying to tack on extensions, and even trying (and failing) to whip up a decent ErrorController.

Categories