是否可以在.cshtml
之类的Views(而不是Views子文件夹)内部使用_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?}");
});
答案 0 :(得分:0)
如果您是从其他位置而不是默认/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);
}
答案 1 :(得分:0)
是的,您可以直接在view文件夹中使用有效的.cshtml,如果您要重定向,为什么不修改RouteConfig.cs。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);