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);
}
}
Related
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
);
I have added the following code
public class RouteController : Controller
{
public ContentResult GetImpression()
{
// Do something
}
}
In RouteConfig class i have added the following
routes.MapRoute(
name: "Impression",
url: "imp",
defaults: new { controller = "Route", action = "GetImpression", id = UrlParameter.Optional }
);
I am expecting my http://mymachine/imp to work. What am i doing wrong? Do i have to do some settings in IIS as well?
For issues with routing I have always found the following tool from Phil Hacck to be invaluable in figuring out where I screwed up with my routing rules and what is getting called. There is nothing special you need to do in IIS to get things working. The one thing you probably should check is to ensure if your Application Pool is using version 4.0 and not version 2.0. Usually when you create a site in IIS it defaults the App Pool to 2.0.
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.
In Visual Web Developer when I "run" my Controller (TestApp) I come up with this:
http://postimage.org/image/iggcs6hw/
I've tried adding "/TestApp" on the end of the local host address in the address bar and that gave me this result:
http://postimage.org/image/ih078cf8/
I don't think I've misspelled anything. Forgive me if this question is a stupid one, just trying to get my feet off the ground :D.
Make sure you have renamed the default ~/Views/Home folder that was generated when you created your project into ~/Views/TestApp folder. As far as the first error message is concerned make sure you have modified the default routes in Global.asax to make the TestApp controller the default one instead of Home:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "TestApp", action = "Index", id = UrlParameter.Optional }
);
So to sum up:
Make sure that you have an Index.aspx view inside the ~/Views/TestApp folder.
Make sure that you have set the TestApp controller in the default route in Global.asax
Make sure that your TestApp controller has an Index action
Now you will be able to call your application like this: http://example.com/ which will automatically call the Index action on TestApp controller which will render the ~/Views/TestApp/Index.aspx view.
Make sure your views are in the /Views directory. MVC follows a strict folder structure and this happens when it can't find something.
When adding a new view or controller to an MVC project, it's best to use the wizard provided for that purpose.
This is from a very good book by Steven Sanderson
I am trying to follow the chapter 4 and trying to setup IOC on my mvc code from the code sample of the book but its not working.
I follow the code from page 97 to page 101 where I set up Inversion of Control and run the code but I get the following error.
A dialog box opens trying to search the following file:
c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\Windsor\WindsorContainer.cs
It seems like controllertype is null in the following line of code:
protected override IController GetControllerInstance(
System.Web.Routing.RequestContext requestContext,
Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
The exception happens at the above return statement saying " {"Value cannot be null.\r\nParameter name: service"}"
This happens to be in WindsorContainerFactory.
Routes looks as follows:
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 = "Products", action = "List", id = ""} // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
}
}
Please help..
Thanks..
Your method is probably being called for requests that are not served by your app. For example for favicon.ico.
Make sure your controllerType is not null. If it is just return null and bypass the code that you added.
Different kind of browsers will make different "extra" requests to your site depending on how they cache. Also, if you are hosting the site with IIS vs Visual Studio Dev Server these might catch the request before it gets to your (e.g. IIS will server a .jpg file without having to letting go to your controller but the VS Dev Server might not)
You could try debugging the route.
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
So far I have found the following links addressing the same exact issue. I am copying them here for my own reference.
http://forums.asp.net/t/1559062.aspx?Pro+ASP.NET+MVC+-+setting+up+IOC+Castle+Windsor
First, I think it will be better if you let the relative link more than absolute link .
Second , check your browsers . I saw no error in your code . maybe different browsers will make it worse. Also, if you are hosting the site with IIS vs Visual Studio Dev Server these might catch the request . :D