Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - Cannot resolve view (controllers and views in separate projects)

I have a Solution with 4 projects:

  • AS.Core.Common (references: Data, Web)
  • AS.Core.Controllers (references: Common, Data, Web)
  • AS.Core.Data
  • AS.Core.Web (references: Data)

In my controllers, anywhere that I return a view:

return View("~/Views/Home/Index.cshtml");

It is highlighted in red and I get a "Cannot resolve view '~/Views/Home/Index.cshtml'".

All four projects build successfully but when I hit F5 I get the following:

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: /

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

How can I get my controllers to see my views so they resolve properly? I'm assuming this is why I get the 404 response.

My Global.asax.cs look like so:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // Register the default hubs route: ~/signalr
        RouteTable.Routes.MapHubs();

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

My RouteConfig.cs (I added namespaces but it appears to have not helped)

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        namespaces: new[] { "AS.Core.Controllers" },
        defaults: new 
        {
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional
        });
}

This is my HomeController.cs

public class HomeController : Controller
{
    public ActionResult Index()
    {
        //return View("~/Views/Home/Index.cshtml");
        return View();
    }
 }
like image 900
Mithrilhall Avatar asked Jun 05 '13 13:06

Mithrilhall


1 Answers

In asp.net-mvc you could return a valid View in the respective controller's views's folder. For sample, if you have a controller called Product, you could have a folder in the path ~/Views/Product/Index.cshtml. In your Index action, you just return a View using the View() method of the Controller class base that all controllers in asp.net mvc should inherits. For sample:

public ActionResult Index()
{
   return View();
}

Asp.Net will find a View on the folder with the same name of your Action, Index in this case.

You also could return another View using the View method since you have the respective View on the folder, for sample:

public ActionResult Index()
{
   return View("About");
}

Considering this case, you should have a View named About in the Product folder. For sample: ~/Views/Product/About.cshtml.

If you change your controllers to another project (class library), you have to set on the asp.net mvc initialization what is the default namespace of your controllers, try something like this on the Global.asax file and Application_Start method:

ControllerBuilder.Current.DefaultNamespaces.Add("NamespaceOfYourProject.Controllers");

Take a look at this article: http://dotnetslackers.com/articles/aspnet/storing-asp-net-mvc-controllers-views-in-separate-assemblies.aspx

like image 145
Felipe Oriani Avatar answered Sep 30 '22 09:09

Felipe Oriani