I am currently working in a brownfield ASP.NET MVC 3 project in VS2010.
In this project, views and controllers are in separate projects. This is not something that I have seen before. In each action method there is no explicit stating of view name as below.
return View("viewName",passingModel);//projects where controllers and views are in same
I have done this implicitly in VS2012 by right clicking on the view and do add view. So I was not bothered about where is this connection between action method's return view and the view is stated.
Unlike in VS2012, in VS2010 I can not navigate to the view that is related to one particular action method by right clicking on View and doing go to view.
I tried to understand this by doing this small experiment. I created a Controller and created a Action Method call xxxx and I created a view for that implicitly as mentioned above and searched the word xxxx in entire solution but this word only appeared in controller and in the view.
So, I was unsuccessful in finding the answer. I think visual studio itself creating its own mapping to achieve this.
I would like to know who these implicit connections are created among action methods and views to understand what is going on in my project.
Edit:
Both the projects which contains controllers and views are class libraries. not asp.net mvc projects.
Global.aspx file contains this:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
protected void Application_Start()
{
DependenciesHelper.Register(new HttpContextWrapper(Context));
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RoutingHelper.RegisterRoutes(RouteTable.Routes);
}
protected void Application_End()
{
//Should close the index
//If this method is not executed, the search engine will still work.
SearchService.CloseIndex();
}
The mapping is fairly straightforward. For example if you have a controller called "MyBrilliantController" and an action method called "MyExcellentAction" which returned just return View(); it would map to (in the UI project) ~/Views/MyBrilliant/MyExcellentAction.cshtml
The only time where this is different is when you are working with "Areas" - but the mapping is effectively the same, it would just consider the area folder first (ie ~/Areas/MyArea/Views/MyBrilliant/MyExcellentAction.cshtml)
Hope that helps.
EDIT - You can also specify namespaces in the global.asax file on each route for the engine to find controllers
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 = UrlParameter.Optional
}, // Parameter defaults
new string[] {
// namespaces in which to find controllers for this route
"MySolution.MyControllersLib1.Helpers",
"MySolution.MyControllersLib2.Helpers",
"MySolution.MyControllersLib3.Helpers"
}
);
}
Related
I've been working on this problem for a while now and I'm not sure what else to try. The error message I'm getting is this:
System.InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
The view is located at Views/Home/Index.cshtml
Here's the RouteConfig class
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Even putting a breakpoint in the Index action on the Home controller doesn't trigger. The Index controller action just returns the view:
public ActionResult Index() {
return View("~/Views/Home/Index.cshtml");
}
The project settings are set to Use Local IIS Web server
Building the project succeeds. There are no build events. Target framework is .NET Framework 4.5. Output type is Class Library.
I was comparing everything I could think of with another project that is working and there's nothing standing out to me. I even compared the .csproj files of both and the only differences seemed to be <Content Include lines for files that are not in the other project. Any ideas on where to look next to fix this?
You should just say return View("Index"); or just return View(); It would search for a view with the same name as of action method
Try right clicking on your index.cshtml and select Properties. In the properties window set the Build Action to Content. This may solve your problem.
Also verify you have the _Layout.cshtml file in the shared folder if your _ViewStart.cshtml points to that.
After deciding to stop focusing on the error and start focusing on why breakpoints were not being hit, I found myself at this answer
Ok so after 4 hours wasted, I find that setting my web project as the startup project solves the issue! This surely must be a bug...
I hope I save someone out there half a day :)
Turns out the website project was not set as the startup project. Unfortunately, other answerer, I still ended up spending way to long figuring that out.
So far when I open root location, it is an admin home page (i.e. www.example.com), and i would like to make an landing page in that root location, but all admin things i would like to have in separate folder (i.e. www.example.com/admin/).
In example routing option for 'uses' currently is www.example.com/uses/ and I would like to have it in www.exaple.com/admin/users, but I dont want to change the project structure only routing. How can i make it with the minimal change?
EDIT:
I have tried registering routes in MvcApplication class in Global.asax.cs, but it didn't work
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
But this didn't work out.
For Asp, I would reference the following exert from Microsoft:
Routes
A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. A handler can also be a class that processes the request, such as a controller in an MVC application. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route.
You add the route to the application by adding the Route object to the static Routes property of the RouteTable class. The Routes property is a RouteCollection object that stores all the routes for the application.
You typically do not have to write code to add routes in an MVC application. Visual Studio project templates for MVC include preconfigured URL routes. These are defined in the MvcApplication class, which is defined in the Global.asax file.
Src. https://msdn.microsoft.com/en-us/library/cc668201.aspx?f=255&MSPPError=-2147217396
What I would do is create an ADMIN controller, and then set up routes something like so: {controller}/{page}/{action}
Alternatively, you can follow this solution to rewrite routes to subfolder: https://stackoverflow.com/a/17074898/2267583
I'm currently working on an MVC project and I'm trying to figure out how I might go about extending the routes of an existing Controller within an Area, specifically from another project.
For instance, I have a Controller with an area that looks like the following :
namespace MyProject.Areas.Foo.Controllers
{
[Authorize]
public class FooController : ApplicationController
{
//code
}
}
And what I would like to do, is be able to define another Controller, within a separate project that could extend this like so :
namespace MyOtherProject.Areas.Foo.Custom.Controllers
{
public class FooController : ApplicationController
{
public string Bar()
{
return "Bar";
}
}
}
Basically, I would like the controllers to almost function as if I was using the partial keyword (so that I could call any of the actions in the original or the new one).
The Main Problem
What I am really trying to accomplish is that I have a main project with several areas and another area of my solution with various client folders. I want to be able to essentially extend the base controllers for my main project and add client-specific actions within these client folders so that they can be used in the main project. I'm already doing this with certain MVC Views, but I was hoping I could accomplish it with controllers as well.
What I've tried
I tried using the partial keyword on both declarations of the class, but since they are in different projects / assemblies, I don't think that works.
I defined a build event that would move the custom DLL into the bin directory of the main MVC project, but that didn't seem to work as expected.
I've tried various approaches for inheritance, hoping the new class would get picked up, but those didn't work (received the duplicate controller declaration error).
I've read about trying to use a custom ControllerFactory but I wasn't sure how to implement it.
I've tried defining custom namespace routing parameters in the AreaRegistration section to pick up the new controller like the following example.
Routing Example (AreaRegistration)
context.MapRoute(
AreaName,
String.Format("{0}/{{action}}/{{id}}", AreaName),
new { controller = AreaName, action = "Index", id = UrlParameter.Optional },
new[] {
String.Format("MyProject.Areas.{0}.Controllers", AreaName),
String.Format("MyOtherProject.Areas.{0}.Custom.Controllers", AreaName)
}
);
Update
I attempted an approach seen here as per some of the comments discussion that involved simply handling this via inheritance :
// Main Project
namespace MyProject.Areas.Foo.Controllers
{
[Authorize]
public class FooController : ApplicationController
{
public ActionResult Index()
{
return View();
}
}
}
// This is in another project / namespace / assembly
namespace MyOtherProject.Foo.Controllers
{
public class CustomFooController : MyProject.Areas.Foo.Controllers.FooController
{
[Route("Foo/Bar")]
public string Bar()
{
return "Bar";
}
}
}
So my current steps are as follows :
Inherited from the base FooController in the main project within another project / solution.
Set up attribute routing to access the custom controller to avoid conflicting routes from the main project.
Created a Build Event that moves the custom DLL into the main project when built (so it will be accessible) from the new custom project.
This didn't seem to make any difference. I tried going to the Foo/Bar url but it just threw a 404 as if it didn't see it at all. The CustomFooController.cs file is in it's own separate project and is just a class file and not an MVC project. Is this correct? Do I need to set the routing rules in the main project?
Controller Inheritance
Using inheritance as Chris mentioned in the comments section will likely be the best way of going about this as well. This is especially true if you are already deriving from another base controller class like ApplicationController in your example :
// ProjectA is assumed to be your "main" MVC application
public class CustomFooController : ProjectA.Controllers.FooController
{
[Route("Foo/Bar")]
public ActionResult Bar()
{
return Content("Bar");
}
}
The attribute routing here is extremely important as you don't want your existing routes to confuse your two controllers or overlook them.
Registering Attribute Routes
Since you are using attribute routing via the [Route] attribute within your ProjectB section, you'll want to ensure that you explicitly set it within the RouteConfig.cs of your ProjectA project so that it can properly identify it through the Routes.MapMvcAttributeRoutes() method as seen below :
public static void RegisterRoutes(RouteCollection routes)
{
// This is important to set up your Route Attributes
routes.MapMvcAttributeRoutes();
// Route declarations omitted for brevity
}
Likewise, if you are using Areas, you'll want to configure this within the respective AreaRegistration.cs file as well :
public override void RegisterArea(AreaRegistrationContext context)
{
// Wire up any attribute based routing
context.Routes.MapMvcAttributeRoutes();
// Area routing omitted for brevity
}
Scoping Routes
Finally, the last thing you'll want to make sure to do is properly "scope" your routes to prioritize your main namespace within the RouteConfig.cs of your main ProjectA application :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Foo", action = "Index", id = UrlParameter.Optional },
// This will prioritize your existing Controllers so they work as expected
namespaces: new[] { "ProjectA.Controllers"}
);
}
Getting References Across
You mentioned using a Build Event to copy over the DLL from your ProjectB project into your main ProjectA project, which should be fine in this case. You will basically need some way to access it and a simply xcopy like the following should be fine in most scenarios :
xcopy /E /Y /S "$(ProjectName).dll" "$(SolutionDir)\ProjectA\Bin\"
Putting It All Together
If you have wired up all of these steps correctly, you should be able to Clean / Rebuild your existing solution. After doing so, double-check to ensure that you have the appropriate DLL within your ProjectA bin directory :
If that is there, then you are on the right track and should be able to run your main application and navigate to ~/Foo to see the following :
Likewise, navigating to ~/Foo/Bar should pick up the appropriate attribute route that was defined in your other Controller and serve the proper content :
I am working on an app with ASP.NET MVC 5. I want my app to have a route that looks like the following:
http://www.myserver.com/my-category
Please notice how the route has a dash (-) in it. I currently have a controller named MyCategoryController. It is defined like this:
namespace MyApp.Controllers
{
[RoutePrefix("my-category")]
public class MyCategoryController : Controller
{
// GET: List
public ActionResult Index()
{
return View();
}
}
}
The view is located in /Views/My-Category/Index.cshtml. When I try to access http://www.myserver.com/my-category in the browser, I get an error that says:
The resource cannot be found.
I set a breakpoint and I noticed that the breakpoint is not hit. I then enter http://www.myserver.com/mycategory into the browser, and I get an error that says:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/mycategory/Index.cshtml
~/Views/mycategory/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
How do I setup my ASP.NET MVC so that
a) I can visit http://www.myserver.com/my-category and
b) Load the view from /Views/my-category/Index.cshtml
You need to name the views folder like the controller not like the route.
So /Views/MyCategory/Index.cshtml and not /Views/My-Category/Index.cshtml.
If you, for a reason I can't imagine why, want it to be /Views/My-Category/Index.cshtml you need to "fully quallify the view":
return View("~/Views/My-Category/Index.cshtml");
About the route with the dash: I am not using attribute based routing so I can only guess:
Did you add the routes.MapMvcAttributeRoutes(); in your RegisterRoutes method?
Because http://www.myserver.com/mycategory is routed by the default "{controller}/{action}/{id}" route...
This is a very strange problem. I am new to the mvc world coming from web forms and i am trying to understand its concepts. Using the MVC template in vs 2013 (premium), I have built a project. In order to see how things work:
I create a new controller 'IndexController' and put it in the folder .../Controllers/IndexController.cs
I create a new View for this controller 'Index.cshtml' and put it in the corresponding folder: .../Views/Index/[#] Index.cshtml
Then I change the routing so that the default routing will now point to this IndexController and not to the default HomeController
Here is my routing table:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "GetIndexPage", id = UrlParameter.Optional }
);
}
}
You can see that I am using 'GetIndexPage' as the default action instead of 'Index' (I'm playing around and see how it works)
Whenerver I make a change in the Index.cshtml (say I add a simple markup and hit 'Run' I always receive the error message
Server Error in '/' Application. The resource cannot be found.
Looking at the address bar I see that the browser is looking for the resource 'localhost:xxx/Index/Index' instead of 'localhost:xxx/Index/GetIndexPage'. To solve this problem, I go in the IndexController and put a breakpoint inthe line
return View(...);
Now I hit 'Run', after stopping at the breakpoint, every thing works perfectly. So it is not a problem of routing since the page is displayed after this breakpoint trick. Visual Studio seems to mess up with the deployment to the IIS Express I am using after I have made a change to the cshtml view. The problem does not occur when I make a change in the code behind of the controller. I don't know where to look at...I have spent this whole night trying to find a solution in Google and stackoverflow...I don't want to reinstall the whole visual studio. Any help, any hint to a certain direction will be greatly appreciated.
After a lot of trials I discover that mvc framework wants the action and the view (that this action returns) must have the same name. In my case this is what has worked:
route configuration:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "IndexPage", id = UrlParameter.Optional }
);
IndexController:
public class IndexController : Controller
{
public ActionResult IndexPage()
{
return View();
}
}
You can see that route action has same name with method of controller: IndexPage
And finally create a view with name IndexPage.cshtml
With this configuration when I make a change in the cshtml file it is immediately reflected in the browser and there is not the error reported above.
I just want to have a confirmation if this is indeed the way things must be set up with the mvc approach. (thanks with your help)