0

I'm migrating an ASP.NET web forms application to ASP.NET MVC 3. I kind of understand routing, but I sort of don't. In my application, I have created three .cshtml files in the directory located at /internal/products/find/. For the sake of demonstration, those .cshtml files are named "view1.cshtml", "view2.cshtml", and "view3.cshtml".

I have a controller named "InternalController". My goal is to use InternalController for all of the locations inside the /internal path. I'm not sure if what I'm trying to do is allowed. I assume it is. Either way, at this time, I have the following in InternalController:

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

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

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

In my global.asax.cs file, I'm trying to register the routes to these views as follows:

routes.MapRoute(
  "View1",
  "{controller}/products/find/view1",
  new { controller = "Internal", action = "View1" }
);

routes.MapRoute(
  "View2",
  "{controller}/products/find/view2",
  new { controller = "Internal", action = "View2" }
);

routes.MapRoute(
  "View3",
  "{controller}/products/find/view3",
  new { controller = "Internal", action = "View3" }
);

Whenever I try to visit /internal/products/find/view1 in my browser, I see the ASP.NET error screen and it says:

The view 'View1' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/internal/View1.aspx ~/Views/internal/View1.ascx ~/Views/Shared/View1.aspx ~/Views/Shared/View1.ascx ~/Views/dashboard/View1.cshtml ~/Views/dashboard/View1.vbhtml ~/Views/Shared/View1.cshtml ~/Views/Shared/View1.vbhtml

What am I doing wrong? The path /internal/products/find/view1 is the most important part for me. Ideally, I would like to expose that in InternalController everytime. But I'm having a rough go at it. What am I doing wrong?

Thanks!

JQuery Mobile
  • 6,221
  • 24
  • 81
  • 134

1 Answers1

0

When you write

routes.MapRoute(
  "View1",
  "{controller}/products/find/{action}",
  new { controller = "Internal", action = "View1" }
);

it means that whenever user writes into his browser:

http://mysite.com/blahblah/products/find/blahblahview

it will activate action view1 inside controller blahblahview. But it doesn't mean that view1.cshtml file is at that path. Actually, asp.net mvc looks for views at directories defined by convention...and convetion is:

~/Views/ControllerName/ViewName

so, your view should be in a folder:

~/Views/Internal/View1.cshtml

Unlike ASP.NET WebForms you are used to, ASP.NET MVC is pretty much driven by naming conventions as you could probably see (you always name your controllers like BlahBlah*Controller*, you always place your views inside Views folder etc... Read some tutorials here and catch up with basics.

Aleksandar Vucetic
  • 14,715
  • 9
  • 53
  • 56
  • My .cshtml files are located in the /products/find directory. How do I update my controllers or routes to use those views? – JQuery Mobile Feb 12 '12 at 14:19
  • I wouldn't do that (just copy your files to default view folder), but if you really want to do it, here is the answer: http://stackoverflow.com/questions/909794/how-to-change-default-view-location-scheme-in-asp-net-mvc – Aleksandar Vucetic Feb 12 '12 at 15:27