mvc改变路由

来源:互联网 发布:免费游戏挂机软件 编辑:程序博客网 时间:2024/05/18 13:12

默认情况下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

原创粉丝点击