mvc改变路由

本文介绍如何在ASP.NET MVC中通过创建自定义视图引擎来改变视图文件的默认查找路径,以实现更灵活的视图组织方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

默认情况下asp.net mvc会去View目录下Controller对应文件夹下查找文件 action名字对应的.cshtml文件

如果需要修改路径,如View/UserCenter/ControllerName/Action.cshtml

需要添加自定义视图引擎

 

protected void Application_Start()
{
    ViewEngines.Engines.Add(new UCEngine());
}


public class UCEngine : BuildManagerViewEngine
{
    private static string[] NewAreaViewFormats = new string[] { "/Views/UserCenter/{1}/{0}.cshtml" };

    public UCEngine()
        : this(null)
    {
    }

    public UCEngine(IViewPageActivator viewPageActivator)
        : base(viewPageActivator)
    {

        ViewLocationFormats = new[] {
            "~/Views/UserCenter/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };

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

        FileExtensions = new[] {
            "cshtml"
        };
    }

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        return new RazorView(controllerContext, partialPath,
                             layoutPath: null, runViewStartPages: false, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator);
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        var view = new RazorView(controllerContext, viewPath,
                                 layoutPath: masterPath, runViewStartPages: true, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator);
        return view;
    }
}

转自:http://hi.baidu.com/makebgf/blog/item/0ff47b146e3cb5daaf513312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值