简体   繁体   English

我什么时候可以使用自己的RouteHandler?

[英]When would I use my own RouteHandler?

I understand that in ASP.Net DynamicData (and maybe regular ASP or MVC) I can provide my own RouteHandler 据我所知,在ASP.Net DynamicData(也许是普通的ASP或MVC)中,我可以提供自己的RouteHandler

routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
    RouteHandler = new CustomRouteHandler() 
});

public class CustomRouteHandler : DynamicDataRouteHandler
{
    public override IHttpHandler CreateHandler(DynamicDataRoute route, MetaTable table, string action)
    {
        // what kind of cool stuff should I add in here?
        return base.CreateHandler(route, table, action);
    }

    protected override string GetCustomPageVirtualPath(MetaTable table, string viewName)
    {
        // what kind of cool stuff should I add in here?
        return base.GetCustomPageVirtualPath(table, viewName);
    }

    protected override string GetScaffoldPageVirtualPath(MetaTable table, string viewName)
    {
        // what kind of cool stuff should I add in here?
        return base.GetScaffoldPageVirtualPath(table, viewName);
    }
}

But can someone explain how I would fill this class out? 但有人可以解释我如何填写这个课程吗? (give some example code) (举一些示例代码)

What would I override to do something useful? 我会覆盖什么来做一些有用的事情?

What sort of things could I do with my own RouteProvider? 我可以用自己的RouteProvider做些什么? Give me examples where this would be useful. 给我一些有用的例子。

As an example, I would like to do a 401 redirect for some tables but continue with the default behavior for other tables (based upon role or logged-in user, of course). 作为一个例子,我想对某些表进行401重定向,但继续使用其他表的默认行为(当然,基于角色或登录用户)。

You could use it for SEO (search engine optimization) in any number of web applications. 您可以在任意数量的Web应用程序中将其用于SEO(搜索引擎优化)。 Something that could have been accomplished with URL Rewriting in the past. 过去可以通过URL重写完成的事情。 For instance, if you were to build a blog engine, and wanted to have a slug in the url that included keywords for your article, you could add this as a handler. 例如,如果您要构建一个博客引擎,并希望在包含文章关键字的网址中有一个slug,您可以将其添加为处理程序。

http://mysite.com/blog/cheap-umbrellas-in-san-diego.aspx http://mysite.com/blog/cheap-umbrellas-in-san-diego.aspx

You can then create a route handler to deal with that particular pattern and look up your post by slug (cheap-umbrellas-in-san-diego) vs by something like ID (/blog/post.aspx?id=123465) 然后,您可以创建一个路由处理程序来处理该特定模式,并通过slug(cheap-umbrellas-in-san-diego)与ID之类的内容查找您的帖子(/blog/post.aspx?id=123465)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM