Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVC5中的routes.MapRoute中等待服务呼叫_C#_Asp.net Mvc 5_Asp.net Mvc Routing_Autofac - Fatal编程技术网

C# 如何在MVC5中的routes.MapRoute中等待服务呼叫

C# 如何在MVC5中的routes.MapRoute中等待服务呼叫,c#,asp.net-mvc-5,asp.net-mvc-routing,autofac,C#,Asp.net Mvc 5,Asp.net Mvc Routing,Autofac,我正在用MVC5编写一个定制CMS,并对其进行大规模检修。这个定制的CMS有“页面”,其中“URL”存储在数据库中。例如,如果用户在浏览器中请求/stackoverflow,并且数据库中有一个页面以/stackoverflow作为url列出,那么我将在指定的CoreCms/Index控制器上提供数据库内容,并将数据库内容作为模型属性进行查看。我的想法是,我可以使用这个单一的控制器/视图来提供数据库中的任何页面 我正在进行的重写仅在服务层中使用依赖项注入和异步调用。似乎我在使用RouteConfi

我正在用MVC5编写一个定制CMS,并对其进行大规模检修。这个定制的CMS有“页面”,其中“URL”存储在数据库中。例如,如果用户在浏览器中请求/stackoverflow,并且数据库中有一个页面以/stackoverflow作为url列出,那么我将在指定的CoreCms/Index控制器上提供数据库内容,并将数据库内容作为模型属性进行查看。我的想法是,我可以使用这个单一的控制器/视图来提供数据库中的任何页面

我正在进行的重写仅在服务层中使用依赖项注入和异步调用。似乎我在使用RouteConfig RegisterRoutes静态方法时遇到了一些问题

这里有一些代码

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

        routes.MapRoute(
            name: "CmsRoutes",
            url: "{*permalink}",
            defaults: new { controller = "CmsCorePage", action = "Index" },
            constraints: new { url = new CmsCoreRouting() }
        );

        routes.MapRoute(
           name: "ArticlesCategoryRoute",
           url: "Articles/{categoryURL}",
           defaults: new { controller = "CmsCoreArticles", action = "Index", categoryURL = UrlParameter.Optional }
        );

        routes.MapRoute(
          name: "ArticlesPostsRoute",
          url: "Articles/{categoryURL}/{postURL}",
          defaults: new { controller = "CmsCoreArticles", action = "ArticlePost", categoryURL = UrlParameter.Optional, postURL = UrlParameter.Optional }
       );

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
请注意,我正在重写旧代码和约束:我相信新的{url=new CmsCoreRouting()}是我打算在这里修改的代码

作为参考,这里是CmsCoreRouting类

public class CmsCoreRouting : IRouteConstraint
{

    private ICoreCmsServices _coreSvc;

    public CmsCoreRouting()
    {
    }

    public CmsCoreRouting(ICoreCmsServices coreSvc)
    {
        _coreSvc = coreSvc;
     }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (httpContext?.Request?.FilePath == null) { return false; }

        string myURL = httpContext.Request.FilePath;

        if (myURL.StartsWith("/"))
        {
            myURL = myURL.Substring(1, myURL.Length - 1);
        }

        myURL = myURL.ToLower();

        var siteId = CoreCms.Core.Settings.CoreCmsSettings.SiteId;

        var cmsPage = AsyncUtility.RunSync(() => _coreSvc.PageService.FindBySiteAndUrlAsync(siteId, myURL));

        if (cmsPage != null)
        {
            return true;
        }

        var cmsArticle = AsyncUtility.RunSync(() => _coreSvc.ArticleService.FindCategoryBySiteAndUrlAsync(siteId, myURL));

        if (cmsArticle != null)
        {
            return true;
        }

        return false;
    }


}
问题:当MVC启动时,它调用CmsCoreRouting上的无参数构造函数(因为我在url=new CmsCoreRouting()中告诉它),但我不确定如何同时使用AutoFac的DI,这样我就不必在这个RegisterRoutes函数上传递我自己的服务实例、存储库实例和DbContext

这方面的任何帮助都会很好。我想“正确”地执行此操作。

请冲突解决程序(AutoFac)为您创建它:

routes.MapRoute(
        name: "CmsRoutes",
        url: "{*permalink}",
        defaults: new { controller = "CmsCorePage", action = "Index" },
        constraints: new { url =  DependencyResolver.Current.GetService<CmsCoreRouting>() }
    );
routes.MapRoute(
名称:“CmsRoutes”,
url:“{*permalink}”,
默认值:新建{controller=“CmsCorePage”,action=“Index”},
约束:新建{url=DependencyResolver.Current.GetService()}
);

谢谢您的帮助。我最后做的是将DependencyResolver放置在CoreCmsRouting>Match方法中,以便将服务定位到更靠近我需要它的位置。