asp.net-mvc-3 - 基于存储在数据库中的 URL 的自定义 MVC 路由

标签 asp.net-mvc-3 routing

我正在尝试根据存储在 mvc 数据库中的 url 添加一些自定义路由逻辑。 (CMS Like),我认为它相当基础,但我觉得我并没有真正取得任何进展。

基本上用户可以输入 url,例如:

www.somesite.com/categorya/categoryb/categoryf/someitem
www.somesite.com/about/someinfo

在数据库中存储了这些项目,以及它们的类型,即普通页面或产品页面。

基于此,我然后想实际点击一个不同的“操作”方法,即我希望上面的点击方法:

PageController/Product
PageController/Normal

这些操作然后加载此页面的内容并显示相同的 View (产品 View 或普通 View )。

使用正常的路由方式是行不通的,因为我可能有类似的事情;

cata/producta
cata/catb/catc/catd/cate/catf/producta

现在我一直在看这里:ASP.NET MVC custom routing for search

并试图以此为基础,但我如何真正“更改”我想在 InvokeActionMethod 调用中调用的操作方法?

顺便说一句,使用 MVC 3.0。

感谢您的帮助/建议

最终解决方案:

全局.asax

routes.MapRoute(
                "Default",
                "{*path}",
                new { controller = "Page", action = "NotFound", path= "Home" }
            ).RouteHandler = new ApplicationRouteHandler();

路由处理器

public class ApplicationRouteHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return new ApplicationHandler(requestContext);
        }
    }

    public class ApplicationHandler : MvcHandler, IRequiresSessionState
    {
        public ApplicationHandler(RequestContext requestContext)
            : base(requestContext)
        {

        }

        protected override IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)
        {
            var url = RequestContext.RouteData.Values["path"].ToString();
            var page = SomePageService.GetPageByUrl(url);

            if (page == null)
            {
                RequestContext.RouteData.Values["Action"] = "NotFound";
            }
            else
            {
                RequestContext.RouteData.Values["Action"] = page.Action;
                RequestContext.RouteData.Values["page"] = page;
            }

            return base.BeginProcessRequest(httpContext, callback, state);
        }
    }

最佳答案

也许不是适合您情况的确切解决方案,但我最近不得不处理类似的事情,因此这可能会为您指明正确的方向。

我所做的是在 Global.asax 中设置一个简单的路由,其中​​包含一个调用自定义 RouteHandler 类的 catch-all 参数。

// Custom MVC route
routes.MapRoute(
    "Custom",
    "{lang}/{*path}",
    new { controller = "Default", action = "Index" },
    new { lang = @"fr|en" }
).RouteHandler = new ApplicationRouteHandler();

应用路由处理器.cs :

public class ApplicationRouteHandler : IRouteHandler
{
    /// <summary>
    /// Provides the object that processes the request.
    /// </summary>
    /// <param name="requestContext">An object that encapsulates information about the request.</param>
    /// <returns>
    /// An object that processes the request.
    /// </returns>
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string path = requestContext.RouteData.Values["path"] as string;
    // attempt to retrieve controller and action for current path
        Page page = GetPageData(path);

    // Method that returns a 404 error
        if (page == null)
            return SetupErrorHandler(requestContext, "ApplicationRouteHandler");

    // Assign route values to current requestContext
        requestContext.RouteData.Values["controller"] = page.Controller;
        requestContext.RouteData.Values["action"] = page.Action;
        return new MvcHandler(requestContext);
    }
}

显然,您从数据库中检索操作和 Controller 名称的方式可能与我的方式大不相同,但这应该能让您有所了解。

关于asp.net-mvc-3 - 基于存储在数据库中的 URL 的自定义 MVC 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12089815/

相关文章:

javascript - 如何更改此 JS 脚本以发出 "GET"而不是 "POST"调用

c# - 显示月份日历模型并与用户交互的更好方法

c# - 为什么尾随 %20(在本例中为有效数据)会终止 asp.net mvc 路由

Angular 2 功能模块路由示例

asp.net - seo 的正确 url 路由

c# - FluentValidation 和服务器+客户端远程验证器

asp.net-mvc-3 - XXXX 不再使用。 <see cref ="EdmModelDiffer"/> 现在用于检测模型中的更改

c# - 如何为整个 Razor View 禁用 HTML 编码

image - 在 vuejs 中的图像中包含路由器链接标签

asp.net - 在帮助类中通过 Page.GetRouteUrl 生成 url 的最佳方法?