MVC3 - The resource cannot be found - c#

I've come across a bit of a weird problem I can't make sense of. One of my controllers has stopped working, but if I rename it then it works fine. I don't have any special routing wrapped around this controller, it just uses my default.
To give specifics, I have a controller called "Kangaroo". In the browser, if I go to {server}/Kangaroo, then I get the "The Resource cannot be found" message. However, if I go to {server}/Kangaroo/Index, then my page loads as normal. I don't have this problem on any of my other controllers, only this one. If I rename the controller (and my view folder) to "Kangaroo2", then it works perfectly fine.
Here is my route:
public class RouteDefinitions {
public static void AddRoutes(RouteCollection routes) {
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapRoute("Resources",
"cache/{action}/{key}/{version}/{type}",
new { controller = "Cache",
action = "CacheContent",
key = "",
version = "",
type = "" });
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "Home",
action = "Index",
id = ""
} // Parameter defaults
);
}
}
Does anyone have an idea of what could be going on here? I thought it might just be a weird visual studio thing, but restarting did not correct the issue.

Just figured out what the problem was. There was a folder in my project called "/Kangaroo". I guess it was treating it like a script or other content. Since the path existed it was attempting to load something from the path.

Related

MVC 4 - issue with routing to non-area controllers

I have a project that I am upgrading from MVC 2 -> MVC 4. During the transition from MVC 2 -> MVC 3, I noticed that some of my hyperlinks broke and were no longer matching the routes as defined before. I have the following project structure:
...
App_Data
Areas
Test
Controllers
TestController
Views
Models
Controllers
PartialController
Views
Scripts
...
The links that are generated are in the format /Test/Partial/Render. If I go back and run the project before the migration, the PartialController Render action is hit as expected. However, now the app says that it can't find an action because there is no Test/Partial controller. I actually am not sure how it worked before, but can't seem to get it to map correctly.
Is there something I'm missing? Here is the relevant code:
Global.asax.cs:
...
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "" }
);
TestAreaRegistration.cs:
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
I did move the Controllers folder up a level to be more in-line with how the project should be structured; beforehand it was in the Areas folder itself. However, none of this worked before I moved that, so I doubt that is the case. I also thought that Autofac had something to do with this, but I am less certain of that because if I shave the "Test" portion out of the URL it matches as expected.
I guess this all boils down to a question on how to check in a "generic" controllers directory first, before matching to an area-specific controller directory, even with an area specified in the URL.
Thanks in advance for your help!
EDIT: If it helps, I noticed that in the existing MVC 2 solution, if I go to Test/Home for example, it will invoke the Index method of the HomeController. However, this does not happen in the new solution. Is this because there is no View associated with the new routes? I have not made new .cshtml files for each of the corresponding .ascx files yet, but I didn't think that should matter much as they are used otherwise.
So apparently the issue was related to namespaces. I had seen that answer on questions like this, but those had to do with collisions finding views. Here is the line I ended up adding to each of my Area registrations:
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "Project.Web.Controllers", "Project.Web.Areas.Test.Controllers" } //added this line
);

Asp.Net MVC3 project Controllers and views are not working

This is kind of an odd problem that I am facing most of the time. I am creating a website and I have created a folder inside controllers folder in the project and I have created a controller inside that folder. So far it was Ok.
Then I created a view for that(Not manually)I can even navigate from controller to view in the code(This is to tell that the controller and the view are properly mapped in the code). But when I run the project and go to that URL its not working. It gives the following error. Although this is a more general error I have no thread to follow to get out of this mess.
But all the things that are created earlier is working smoothly. I have even tried a Default controller and created a view for that and then ran the program and it gives the same error.
Now I can not create new controllers and views(I can but they are not working). I am stuck with this all day long :(.
I feel like some configuration is missing. But I can not find out.
Since I am new to this I am totally lost. And I can not figure out what to do.
I am totally confused and I have no idea of what has happened.
Is it a settings problem in Visual studio?. And what should I do to make this work.
The error is this:
PS: I am working with Team Foundation server and I can not even do debugging. These controller methods are not called.
Have a look into MVC routing and modify your Global.asax.cs with this code:
public class MvcApplication : System.Web.HttpApplication
{
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
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}

MVC route where path exists

I'm trying to create a route that will caption a url like
http://mysite.com/tech/
but I also have an actual directory under the site that is /tech, which contains other static resources. Not my design choice, but I'm migrating an older site to mvc and dont want to break a bunch of very old links.
My route seems to keep failing, and what I can figure is that the server sees this directory and is trying to serve up the default file from it... but none is found, and it fails.
I'd like it to use my route instead and let me serve up my view. Any best practices for this?
UPDATE:
I tried using RouteExistingFiles = true, which made this work, but then I had issues with files which existed inside these directories. (for instance: http://mysite.com/tech/image.jpg). I started adding IgnoreRoute entries for all the various file types that I wanted ignored (ie .jpg files..), but this turned into a big mess. There's gotta be a better way?
You need to set the RouteExistingFiles property of your RouteCollections object to true. This will make your routing override the physical location if a clash is found.
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
routes.RouteExistingFiles = true;
To selectively ignore files/extensions you can use :
routes.IgnoreRoute("{*staticfile}", new { staticfile = #".*\.(css|js|gif|jpg)(/.*)?" });
Another approach which might help if you don't like the ignoreRoute approach, is to extend the ViewEngine and override the file exists implementaion with your own rules.
public class MyViewEngine : RazorViewEngine
{
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
//Some Logic to check for file
}
}
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine());

MVC.ASP.NET and problem with route

Now I have to fix some bugs in MVC.Asp.Net project. In projec i have route:
Global.asax
routes.MapRoute(
"Category no name",
"_C{id}.cfm",
new { controller = "Products", action = "Category" },
new { id = #"\d+" }
);
Controller:
public ActionResult Category(int id)
{
// Break point here
// doing smth
return View();
}
In view I have link:
#category.Name
Everything is working correct when I use Visual Studio Development Server but when I try to run project on Local IIS server ( I use http://localhost/TestSite) I have problem with this link. It doesn't work and I can't fall to break point in my controller.
Why do I have this behaviour? May be smth wrong with my link? I can't change route in global.asax.
Thanks, for any ideas
I suggest that the IIS is not configured properly. Try to set integrated mode for the IIS pool. Or map .cfm extension to ASP .NET.

Matching route in ASP.NET MVC 3 returns a 404

So, all of the sudden when I try to access a newly created controller in my ASP.NET MVC 3 app, it returns a 404 error. I've made sure that the controller is named properly, the route match according to the RouteDebugger, and I've confirmed that a folder for the controller exists in the Views folder and that the properly named View for the action also exists. All of this is in an Area. Oh, and I'm running this on the Visual Studio dev server, not on IIS.
So, I've been at it for about 2 hours and am not getting anywhere, so I'm hoping someone here can point me in the right direction. Here's the source for the controller and routes:
// AdministrationAreaRegistration.cs
public override void RegisterArea(
AreaRegistrationContext AreaRegistrationContext) {
AreaRegistrationContext.MapRoute("8UVhDc", "Administration/{controller}/{DocumentTypeId}", new {
action = "List"
}, new {
controller = #"Documents",
DocumentTypeId = #"\d+"
});
}
// DocumentsController.cs
public sealed class DocumentsController : AdminController {
[Inject]
public DocumentsController(
CookieManager CookieManager,
DocumentTypeManager DocumentTypeManager)
: base(CookieManager: CookieManager, DocumentTypeManager: DocumentTypeManager) {
}
[HttpGet]
public ActionResult List(
short DocumentTypeId) {
return this.View(new AdministrationView {
Cookie = base.Cookie,
DocumentTypes = base.DocumentTypes
});
}
}
And ofcourse, there's a folder named "Documents" in the "Views" folder, and in the "Documents" folder there is a view named "List". As far as I can tell, I shouldn't be getting this error (that probably is something a programmer shouldn't say?)...
Anyway, it would be super-duper awesome if someone points out what I'm obviously screwing up. Thanks in advance!
Make sure that the DocumentsController is defined in the XXX.Areas.Administration.Controllers namespace where XXX is the name of your application. Also make sure that the url you are requesting matches the route constraints:
http://localhost:35076/Administration/Documents/123
please see the error 404 here http://support.microsoft.com/kb/315122

Categories

Resources