I have a C# MVC Razor site. Typically, Controllers load views from the Views folder. However, I have a special circumstance where I need to render a view outside of the Views folder. How do I do that?
Controller will load /Views/Random/Index.cshtml
Can't load /Random/Index.cshtml
/Random/test.aspx loads with no issues, but can't change cshtml files to aspx files, they need to be built regularly.
I have tried return Redirect("/Random/Index.cshtml") in the Controller, and currently have no controller at all.
The weird thing is it works on my Production environment, but not in localhost. In localhost I get:
The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect. Please review the URL below and make sure that it is spelled correctly.
Requested URL: /Random/Index.cshtml
You can definitely do this. For doing this you need to create one new custom view engine like
public class MyViewEngine : RazorViewEngine
{
private static string[] AdditionalViewLocations = new[]{
"~/Random/{0}.cshtml"
};
public MyViewEngine()
{
base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(AdditionalViewLocations).ToArray();
base.ViewLocationFormats = base.ViewLocationFormats.Union(AdditionalViewLocations).ToArray();
base.MasterLocationFormats = base.MasterLocationFormats.Union(AdditionalViewLocations).ToArray();
}
}
Then in you global.asax's Application_Start method register this view engine like this-
ViewEngines.Engines.Add(new MyViewEngine ());
If you want your viewengine to take precedence then insert this at 0th position. like this -
ViewEngines.Engines.Insert(0, new MyViewEngine());
return View("~/AnotherFolder/Index.cshtml")` should work for you.
Do not forget to indicate the Layout in your index view:
#{
Layout="~/Views/Shared/Layout.cshtml";
}
Related
This question already has answers here:
How to serve html file from another directory as ActionResult
(6 answers)
Closed 5 years ago.
We have a .NET 4.0, MVC 2 project, where the HomeController looks like this:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View("~/client/index.html");
}
}
All is fine.
But, when we start linking to .NET 4.5 and MVC 4, the runtime can't seem to find this index.html! We get this error:
The view '~/client/index.html' or its master was not found or no view
engine supports the searched locations. The following locations were
searched: ~/client/index.html
How could this be! What might we be missing here.
I've never seen MVC using straight html pages. This is a more typical setup:
Controller
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View("Index");
}
}
The view would be /views/Home/Index.cshtml. By default MVC wants views to be in the views folder in a folder corresponding to the controller name.
If you want to have the layout broken into a separate file it would typically be in /views/Shared/.
If your HTML page is self-contained you should be able to move it and rename it to index.cshtml and add the following somewhere on the page.
#{
Layout = null;
}
Take care when locating your html page. Placing it inside a view folder where, by the MVC rules, a controller would be expected to do the handling, causes errors in my testing. Here's one way to make it work, though this isn't really coding to MVC pattern:
You can then reference the page:
Notice I've stepped outside the controller/view structure (not recommended for MVC)
To make it fail, which I'm assuming is similar to what is happening for you:
(notice the html is placed in a view where we'd expect a matching controller method to serve it to a caller)...
But...
So if you insist on going this approach perhaps you can set up a content folder outside your MVC controller/view structure and place your html there. But, again, not to beat the subject to death, you could easily convert this to cshtml and serve it up via a simple method in a controller. Just my two cents' worth..
I added MVC to my existing webforms project. All is going well except for RenderAction is looking for .aspx files
The view '_Mainmenu.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/NewPages/Views/Shared/_Mainmenu.cshtml.ascx
The view is
~/Areas/NewPages/Views/Shared/_Mainmenu.cshtml
And it does exist in that folder. Can anybody help me sort this out.
Everything else MVC is working OK I even have PITA EntityFramework working too
Any help would be appreciated
The view '[viewname]' or its master was not found or no view engine supports the searched locations indicates that you're using default view engine which prioritizes web forms view engine (the path shown as ~/Areas/NewPages/Views/Shared/_Mainmenu.cshtml.ascx means that MVC view engine prioritizes searching ASPX & ASCX files instead of Razor cshtml files). To change this behavior which MVC uses Razor view engine by default, insert these lines into Application_Start method on Global.asax:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
// ViewEngines.Engines.Add(new WebFormViewEngine()); => optional webforms engine registration
Additionally, if default Razor view engine still can't recognize cshtml files in areas properly, you need to create a custom view engine class which inherits RazorViewEngine and setting AreaViewLocationFormats in its constructor like this:
public class CustomViewEngine : RazorViewEngine
{
public CustomViewEngine()
{
// Route parsing convention for view engines:
// {0} means action method name
// {1} means controller class name
// {2} means area name
AreaMasterLocationFormats = new[]
{
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
// other view search locations here
};
AreaPartialViewLocationFormats = AreaViewLocationFormats;
}
}
Note that the custom view engine will search all view pages inside areas specified by controller action method depending on routes defined in AreaViewLocationFormats.
Then, register custom view engine class at the same place as RazorViewEngine, i.e. in Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// clear all view engines repository first
ViewEngines.Engines.Clear();
// register Razor view engine only
ViewEngines.Engines.Add(new RazorViewEngine());
// register custom view engine class here
ViewEngines.Engines.Add(new CustomViewEngine());
// other initialization codes here
}
Similar issues:
ASP.NET MVC: When should I create custom View Engine
How do I implement a custom RazorViewEngine to find views in non-standard locations?
In the "Global.asax" file, find "Application_Start()"
Then, Please input this code --
RemoveWebFormEngines();
I have been asked to work on already existing code for a .NET MVC Application, and am required to add a new page. I have done this, but am having issues.
The application has a common layout defined in _Layout.cshtml page (I need to use the same layout for my new page). The path is present in Viewstart.cshtml page.
This is how an existing page works:
The controller has this code:
public ViewResult Index()
{
TransactionHistoryViewModel model = new TransactionHistoryViewModel();
model.SearchType = "TransactionHistory";
return View(model);
}
When the code is run, immediately after executing the above lines, it goes on to the _ViewStart.cstml file and executes the following:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
But when I try to use the same logic for the controller I have created (I have already set up the models):
public ViewResult Index()
{
TransportPlannerViewModel model = new TransportPlannerViewModel();
model.SearchType = "TransportPlanner";
return View(model);
}
the code stops executing after the above block, and does not move on to the _ViewStart file (I figured this out using breakpoints).
I am required by my employers to maintain consistency with the existing code, so I have tried to stick to the original coding style. Could this be an issue with wrong model mapping or setup?
If you have 2x public ViewResult Index() in the same controller, it is not going to work, you will confuse the application about which method signature to choose, because they are identical.
You should have your TransportPlannerViewModel code in a seperate controller. and this should be pointing to a different View Folder associated with that controller.
Ie. If the controller for the first code block is TransactionHistory, then it will be looking for a view under Views\TransactionHistory\Index.cshtml.
By default it uses the pattern Views\ControllerName\ActionName (methodName) to look for the view.
So to get it to work for your other method, if I understand what you are trying to do correctly, you should have a TransportPlanner Controller, then it will look for your view under Views\TransportPlanner\Index.cshtml.
Please clarify if this is not what you are wanting to do.
I'm trying to set the layout path in a custom ActionFilterAttribute I have written as follow:
public class LayoutInjecterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var result = filterContext.Result as ViewResult;
if (result != null)
{
result.MasterName = "~/Views/Layouts/Test.cshtml"
}
}
}
In here Test.cshtml is precompiled view (with the help of RazorGenerator) in a different project.
But it gives me the error:
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.cshtml
~/Views/Shared/Index.cshtml
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Layouts/Test.cshtml
and controller actually is simple:
[LayoutInjecter]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
The error shows that LayoutInjecter is working fine. You said:
In here Test.cshtml is precompiled view in a different project.
But, Using razor views from a different (from outside the web project) is not supported out of the box. However there's a tool to precompile razor views and then you can put them in any DLL which called RazorGenerator.
The compiler can't find the specified master layout file and shows this error.
For more information look at
Precompiled Razor View Using RazorGenerator MVC and
PreCompiledViewEngine in MVC 4.
Compile your asp.net mvc Razor views into a seperate dll
Edit: How did the PrecompiledMvcViewEngine know which view to render?
PrecompiledMvcViewEngine still relies on the ASP.NET MVC Views folder convention, using relative file paths to locate the views. However, this is slightly misleading. The PrecompiledMvcViewEngine doesn’t look at physical files; it looks for the System.Web.WebPages.PageVirtualPathAttribute that the Razor Single File Generator adds to every view that it generates that includes the view’s relative file path.
Edit 2: I believe the guidance for your problem would be found in GitHub.
It works. Make sure the layout path "~/Views/Layouts/Test.cshtml" is correct.
Also, make sure that "Test.cshtml" is a layout page, and not a view / partial view.
Change result.MasterName = "~/Views/Layouts/Test.cshtml" to result.MasterName ="~/Views/Shared/Test.cshtml".
The Framework by convention looks in the ~/Views/Shared/ directory in your asp.net mvc solution for your layout pages.
It appears to me you are dynamically or at runtime selecting a master page.
So I want to have a status page that will show a different layout of the equipment depending on who's using it which will be a variable defined in the web.config. Should I be creating a separate controller per view? Some of the background functions should be similar but some will probably be different in the future. Or should I have the same cshtml file and hide html markup depending on who's using it or not?
I was thinking of doing something like:
#if(System.Configuration.ConfigurationManager.AppSettings["IsSuperUser"] == "true")
{
Status
}
else {
Status
}
Or is this a bad idea?
There are several options, it all depends on your needs and preferences.
Your code will work, however you must also double check permission in your controller! For example, your url will be "/SuperUser/Status" and "/User/Status". Now, what's stopping non-super user to type in "/SuperUser/Status" to the address bar?
One important rule, never trust the end users! Assume that they will not do what you intend them to do.
Given all, my preference would be to include a variable in your Model to identify the user level (super vs non super), then use that to determine the layout in your views. Remember, you can also change the layout of the view based on variable/expression.
#Layout = Model.IsSuperUser ? "_SuperLayout.cshtml" : "_RegularLayout.cshtml";
Sounds like a view concern. I would pass the config data through a dependency in the controller and render partials:
#if (Model.IsSuperUser)
{
#Html.Partial("_SuperUser")
}
else
{
#Html.Partial("_User")
}
The controller can then do something like:
public ActionResult Index()
{
var vm = new MyViewModel();
vm.IsSuperUser = _config.GetSuperUser();
return View(vm);
}