C# 在“视图”文件夹中创建视图
是否可以在视图(不是视图子文件夹)中使用工作的C# 在“视图”文件夹中创建视图,c#,asp.net-mvc,C#,Asp.net Mvc,是否可以在视图(不是视图子文件夹)中使用工作的.cshtml,例如\u ViewImports,但使用我自己的视图。或者可以从/Views文件重定向到/Views/Subfolder视图吗 我找到了解决办法。只需在Startup.cs中现有MapRoute之前添加MapRoute,如下所示: app.UseMvc(routes => { // Short link to a method routes.
.cshtml
,例如\u ViewImports
,但使用我自己的视图。或者可以从/Views
文件重定向到/Views/Subfolder
视图吗
我找到了解决办法。只需在Startup.cs中现有MapRoute之前添加MapRoute,如下所示:
app.UseMvc(routes =>
{
// Short link to a method
routes.MapRoute(
"AnyName",
"NameOfYourIActionResult",
new {controller = "Controller_Name.cs", action = "IActionResult Method"}
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
如果您的意思是从另一个位置返回视图,而不是返回默认的
/view
文件夹,则可以
可以显式指定要使用的视图:
public ActionResult Index()
{
return View(@"~/Your/View/Location/Index.cshtml");
}
或通过自定义视图引擎:
protected void Application_Start()
{
RazorViewEngine rve = new RazorViewEngine();
rve.PartialViewLocationFormats =
rve.ViewLocationFormats =
rve.MasterLocationFormats =
new string[] {
"~/Your/View/Location/{1}/{0}.cshtml",
"~/Your/View/Location/Shared/{1}/{0}.cshtml"};
rve.AreaMasterLocationFormats =
rve.AreaPartialViewLocationFormats =
rve.AreaViewLocationFormats =
new string[] {
"~/Your/View/Location/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Your/View/Location/Areas/{2}/Views/Shared/{1}/{0}.cshtml"};
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(rve);
}
是的,您可以在视图文件夹中直接使用.cshtml,如果您想重定向,为什么不修改RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
是的,在视图文件夹中可以有一个工作的
.cshtml
文件。至于重定向,甚至不知道你的意思。如果这是您的意思,您可以通过传递子文件夹的路径来渲染视图。。。