When I try to make a new view in an area in MVC 6 it only displays a white page. The Home/Index action works fine, and this one will hit the controller but never displays the view. I can return content and get a display, but when I try to return the view it breaks. Any advice?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using PmData.Models;
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace PlantManagement.Areas.Cms.Controllers
{
[Area("Cms")]
public class AssetsController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
}
}
that is coupled with a blank view that calls to the main layout page.
I found the issue. Sadly, it's because I'm so new to MVC 6 / vNext so I feel silly. It was a matter of their being an issue with an item on the page, but without app.UseDeveloperExceptionPage(); being added in the configure of startup.cs it would never show me the actual error, just give me the generic 500 error and white page. Once I added that it started producing errors I could work with and gave me what I needed.
I had a similar issue, and resolved it by,
a) Create a _ViewStart.cshtml file in each area you have. i.e. Areas/Cms/Views/_ViewStart.cshtml
b) In this _ViewStart.cshtml file add
#{
Layout = "~/Areas/Cms/Views/Shared/_LayoutCms.cshtml
}
c) Add a _LayoutCms.cshtml to Areas/Cms/Views/Shared
d) In this file add the reference to the overall site layout
#{
Layout = "~/Views/Shared/_Layout.cshtml
}
and any other area specific layout code.
That fixed my blank page issue. Hopefully yours too
In addition to my last answer, try these steps
e) Make sure you have an area registration setup within your Cms area folder i.e Areas/Cms/CmsAreaRegistration.cs
public class CmsAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Cms";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Cms_default",
"Cms/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
f) In your App_Start/RouteConfig.cs make sure you are registering all areas by adding AreaRegistration.RegisterAllAreas() something like the following.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas(); //Add this//
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Related
I have been working for ~1 year with MVC now but haven't really ever had to look deep into routing and just used the normal Defaults. In a new Application I now need to adjust these and cant figure out how it exactly works.
My Situation
I have an application starting with a View with a List of different Calendars in a DB.
Each of these Calendars should have a url to them like the following:
home/calendarName/weekNumber
--> controller/dynamic string based on name of calendar/dynamic int based on selected week
The issue is that I don't want to create a action for every single CalendarName--> calendar.
Is my Problem understandable?
This is my Startup.cs Code atm:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "test",
template: "test",
defaults: new { controller = "Home", action = "Name-Of-Calender", id="WeekID_Of_Calendar" });
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=KalenderListe}/{id?}");
});
This is the URL-Action in the View atm.
"#Url.Action("View", "Home", new {KalenderName= kalender.KalenderName,Id= kalender.KalenderId})"
If it isn't clear what im trying to do, let me know and I'll adjust it. Thanks in advance.
For your problem you can use Attribute routing on action method. Just write the string in your route just like below
`
[Route("Home/About")]
public IActionResult MyAbout()
{
return View("About");
}
[Route("Home/Contact")]
public IActionResult MyContact()
{
return View("Contact");
}
`
You can also change as you want like
`
[Route("About/Home")]
public IActionResult MyAbout()
{
return View("About");
}
`
You can use attribute routing on your action within your home controller.
[HttpGet("{calendarName}/{id}")]
public void ActionResult GetCalendar(string calendarName, int id)
{
...
}
Or if you want to pass the as a optional query
[HttpGet("{calendarName}")]
public void ActionResult GetCalendar(string calendarName, [FromQuery]int id)
{
...
}
This post is directly related to: MVC5 Area not working
That post fixed the index.cshtml issue, however it did not resolve each view for that controller. For example:
http://localhost:45970/Setup/Start gives the error that the resource cannot be found (basically a 404).
However http://localhost:45970/Setup/Setup/Start brings up the correct page.
So what needs to be reconfigured so that ALL views for that controller in the Setup Area will open properly?
Edit 1
Code from SetupAreaRegistration.cs
using System.Web.Mvc;
namespace BlocqueStore_Web.Areas.Setup
{
public class SetupAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Setup";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Setup_default",
url: "Setup/{controller}/{action}/{id}",
defaults: new { action = "Index", controller = "Setup", id = UrlParameter.Optional },
namespaces: new[] { "BlocqueStore_Web.Areas.Setup.Controllers" }
);
}
}
}
Since you have an Area named Setup with the above route configuration, opening http://localhost:45970/Setup/Start will execute StartController under Setup Area. You got 404 error because you don't have StartController under Setup Area, but you can open http://localhost:45970/Setup/Setup/Start successfully because you have SetupController and Start action method under Setup Area.
Based on your comment, you want the following url patterns
http://{host}/Setup/{view}
http://{host}/Admin/{view}
You can accomplish that without using any Area. You only need AdminController and SetupController using the default route.
I have an asp.net Webforms website that I'm trying to integrate MVC to. I'm almost there, but I'm having some problems.
What I did so far:
Added references to:
System.Web.Routing
System.Web.Abstractions
System.Web.Mvc
Updated the root Web.config to load the three assemblies at run time.
Added directories:
Views
App_Code\Controllers
Updated the Global.asax and configured routing:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
So far so good.
I right-click the Controllers folder, add a "HomeController", like this, run the website and it works!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
public class HomeController : Controller
{
public string Index()
{
return "This is my <b>default</b> action...";
}
public string Welcome()
{
return "This is the Welcome action method...";
}
}
Now the problem:
I update the HomeControler to use a View, like this, but it doesn't work.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
To create the view, I created a directory inside "Views" called "Home". Inside "Home" I added a new "Empty Page (Razor v3)", called "Index.cshtml":
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>View Template!</p>
The first problem is with this line:
ViewBag.Title = "Index";
VS indicates that "The name 'ViewBag' does not exist in the current context
The second problem is that when I run the website, I get the following error message:
The view 'Index' or its master was not found. The following locations
were searched: ~Views/Home/Index.aspx ~Views/Home/Index.ascx
~Views/Shared/Index.aspx ~Views/Shared/Index.ascx
I know it is possible to integrate a Webforms website with MVC pages.
It appears like my configuration is correct. I can successfully add a Controller and get the desired result.
BUT, how do I get my Controller to use a View?
I'm trying to use route attributes in MVC 5. I created an empty MVC project in Visual Studio to experiment with and so far I can't get the routing to work. I'm using this page as a reference. I have the latest assemblies and updated all NuGet packages to their latest versions.
Here's my code:
// RouteConfig.cs
namespace MvcApplication1
{
using System.Web.Mvc;
using System.Web.Routing;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Enables MVC attribute routing.
routes.MapMvcAttributeRoutes();
// The default route mapping. These are "out of the bag" defaults.
routes.MapRoute(null, "{controller}/{action}/{id}", new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
}
}
// TestControler.cs
namespace MvcApplication1.Controllers
{
using System.Web.Mvc;
public class TestController : Controller
{
public ContentResult Output1()
{
return Content("Output 1");
}
[Route("Test/Output2")]
public ContentResult Test2()
{
return Content("Output 2");
}
}
}
#* Index.cshtml *#
#Html.Action("Output1", "Test")
#Html.Action("Output2", "Test")
The Output1() method renders properly. However, when Output2() is rendered, I get the error "A public action method 'Output2' was not found on controller 'MvcApplication1.Controllers.TestController'."
Your action is named Test2 not Output2. Change the following
#Html.Action("Test2", "Test")
This is because #Html.Action will not actually use routing. With it you explicitly specify actions and controllers.
The routing will be used when someone for instance makes the http://example.org/Test/Output2 request from a browser.
I am stuck in simple problem and i am not sure how to solve this.
The view Category or its master was not found or no view engine supports the searched locations. The following locations were
searched:
~/Views/ShoppingCart/Category.aspx
~/Views/ShoppingCart/Category.ascx
~/Views/Shared/Category.aspx
~/Views/Shared/Category.ascx
~/Views/ShoppingCart/Category.cshtml
~/Views/ShoppingCart/Category.vbhtml
~/Views/Shared/Category.cshtml
~/Views/Shared/Category.vbhtml
I have a area called ShoppingCartArea and i have view (Category.cshtml) and controller inside it. But i am not sure how it is referencing to main view folder.
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "ShoppingCart", action = "Category", id = UrlParameter.Optional }, // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
and my area registration.cs
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"ShoppingCartArea_default",
"ShoppingCartArea/{controller}/{action}/{id}",
new { controller = "ShoppingCart", action = "Category", id = UrlParameter.Optional },
new[] { ShoppingCartAppMVC.Areas.ShoppingCartArea.Controllers}
);
}
Lemme know if am doing something wrong
Probably, you ran into this because you have added your areas manually. Recently I have been rebuilding old WebForms applications and slowly transforming it into MVC. I have not added MVC type to a project and just added needed folders etc. Unfortunatelly, I`ve faced the same issue as you.
The answer was to add correct project type (see this answer) and add areas from the
context menu of the project. After that I`ve moved everything to a newly created area - and it worked.
So the answer is that MVC not only creates all the folders, but also updates its routes.