gpt4 book ai didi

c# - ASP.NET MVC 自定义 View 路由

转载 作者:行者123 更新时间:2023-11-30 13:26:33 24 4
gpt4 key购买 nike

我们的解决方案层次结构如下:

Controller \类别\ View

例如: Controller \数据分析\数据检索

现在我想映射路由,这样当用户在url中输入 View 的名称时,它会自动将url映射到相应的 Controller

即:localhost:1234\DataAnalysis\DataRetrieve

应该映射到

查看\DataAnalysis\DataRetrieve\Index.cshtml

类似地,任何包含 Action 的 url 请求都应该检索相应的 View

即:localhost:1234\DataAnalysis\DataRetrieve\TestAction

应该映射到

查看\DataAnalysis\DataRetrieve\TestAction.cshtml

目前,我们使用的是默认路由

    public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
}

这意味着如果我键入上面显示的 URL,它会忽略类别并且无法返回 View 。

有没有办法自定义路由以获得上述行为?

--编辑

为了根据评论澄清我的问题,这是我的解决方案资源管理器的屏幕截图

enter image description here

现在,如果我调用 localhost:12346/DataAnalysis/DataRetrieve,这应该会带我到索引。路由这不是问题,因为我可以这样做:

        routes.MapRoute(
name: "ExampleRouting",
url: "{category}/{controller}/{action}"
);

但是问题来了。我还想像这样组织我的文件结构:

enter image description here

默认情况下,当我尝试检索 dataretrieve 的索引时,它会在 Views\DataRetrieve\Index 下查找,而不是 Views\DataAnalysis\DataRetrieve\Index。

我怎样才能改变这种行为?

编辑2------------------------

根据答案,我添加了一个自定义 View 引擎,在 Application_Start 中注册了它,更新了我的路由。仍然有相同的问题。

全局.asax

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
ViewEngines.Engines.Add(new SPCViewEngine());

BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}

路由配置.cs

    public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


routes.MapRoute(
name: "Default",
url: "{controller}/{category}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", category = "Login", id = UrlParameter.Optional }
);
}

SPCViewEngine(自定义 View 引擎)

public class SPCViewEngine : RazorViewEngine
{
public SPCViewEngine()
: base()
{
ViewLocationFormats = new[] {
"~/Views/{1}/%1/{0}.cshtml",
"~/Views/{1}/%1/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};

PartialViewLocationFormats = new[] {
"~/Views/%1/{1}/{0}.cshtml",
"~/Views/%1/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
}

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
var categoryName = controllerContext.RouteData.Values["category"].ToString();
return base.CreatePartialView(controllerContext, partialPath.Replace("%1", categoryName));
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
var categoryName = controllerContext.RouteData.Values["category"].ToString();
return base.CreateView(controllerContext, viewPath.Replace("%1", categoryName), masterPath);
}

protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
var categoryName = controllerContext.RouteData.Values["category"].ToString();
return base.FileExists(controllerContext, virtualPath.Replace("%1", categoryName));
}
}

最佳答案

如果我正确理解了您的问题,您可以创建自己的 View 引擎,在运行时解析 View 位置并插入您的应用程序。

创建您自己的自定义 View 引擎。

    public class MyViewEngine : RazorViewEngine
{
public MyViewEngine()
: base()
{
ViewLocationFormats = new[] {
"~/Views/{1}/%1/{0}.cshtml",
"~/Views/{1}/%1/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};

PartialViewLocationFormats = new[] {
"~/Views/%1/{1}/{0}.cshtml",
"~/Views/%1/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
}

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
var catagoryName = controllerContext.RouteData.Values["category"].ToString();
return base.CreatePartialView(controllerContext, partialPath.Replace("%1", catagoryName));
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
var catagoryName = controllerContext.RouteData.Values["category"].ToString();
return base.CreateView(controllerContext, viewPath.Replace("%1", catagoryName),masterPath);
}

protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
var catagoryName = controllerContext.RouteData.Values["category"].ToString();
return base.FileExists(controllerContext, virtualPath.Replace("%1", catagoryName));
}

}

并在这里注册

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);


//Register your View Engine Here.
ViewEngines.Engines.Add(new MyViewEngine());
}

更新路由配置,默认应该是

        routes.MapRoute(
name: "Default",
url: "{controller}/{category}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", category = "DefaultCategoryName", id = UrlParameter.Optional }
);

关于c# - ASP.NET MVC 自定义 View 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21322756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com