c# - 在 Views 文件夹中创建一个视图
问题描述
是否可以.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?}");
});
解决方案
如果您的意思是从另一个位置而不是默认/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);
}
推荐阅读
- angular - 如何解决在 Angular 6 中为业力测试抛出的 [object ErrorEvent]
- themes - 将类添加到 Shopware 主题插件
- java - 什么时候使用 Reactor 处理器有意义?
- ios - 使用 CocoaPods 指定库的版本
- ios - 尝试上传 Xcode 项目时出错:请更新 Xcode 并重建您的应用
- exchangewebservices - php -ews - 空 ItemId
- react-native - 如何在本机反应中将表单数据从screen1传递到screen2?
- android - 如何在android中显示带有图像和细节的网站网址
- android - Android:定义 layout_behaviour 路径
- java - 无法访问模态窗口,操作不起作用