0

我已經設置了溫莎堡和WebApi後閱讀它在下面的2個職位。這是我的一個我的設置的亮點:與WebApi 2,溫莎堡,玻璃映射器路由錯誤

參考文章:

代碼設置:

public static class GlassMapperScCustom 
{ 
    public static void CastleConfig(IWindsorContainer container) 
    { 
     container.AddFacility<TypedFactoryFacility>(); 

     var config = new Config 
     { 
      UseWindsorContructor = true 
     }; 

     //MVC 
     container.Register(Component.For<SitecoreController>().LifestyleTransient()); 
     container.Register(Types.FromThisAssembly().BasedOn<Controller>().LifestyleTransient()); 
     DependencyResolver.SetResolver(new WindsorMvcDependencyResolver(container)); 
     ControllerBuilder.Current.SetControllerFactory(new WindsorMvcControllerFactory(container.Kernel)); 

     //WebApiInstaller 
     container.Register(Types.FromThisAssembly().BasedOn<ApiController>().LifestyleTransient()); 
     var resolver = new WindsorResolver(container); //Shown Below 
     GlobalConfiguration.Configuration.DependencyResolver = resolver; 
     GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorWebApiControllerActivator(resolver)); 
    } 
} 
public class WindsorMvcDependencyResolver : IDependencyResolver 
{ 
    private readonly IWindsorContainer _container; 

    public WindsorMvcDependencyResolver(IWindsorContainer container) 
    { 
     if (container == null) throw new ArgumentNullException("container"); 
     _container = container; 
    } 

    public object GetService(Type serviceType) 
    { 
     return _container.Kernel.HasComponent(serviceType) ? _container.Resolve(serviceType) : null; 
    } 

    public IEnumerable<object> GetServices(Type serviceType) 
    { 
     return _container.ResolveAll(serviceType).Cast<object>().ToArray(); 
    } 
} 

public class WindsorMvcControllerFactory : DefaultControllerFactory 
{ 
    private readonly IKernel _kernel; 

    public WindsorMvcControllerFactory(IKernel kernel) 
    { 
     this._kernel = kernel; 
    } 

    public override void ReleaseController(IController controller) 
    { 
     _kernel.ReleaseComponent(controller); 
    } 

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
    { 
     if (controllerType == null) 
     { 
      throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", 
       requestContext.HttpContext.Request.Path)); 
     } 
     return (IController)_kernel.Resolve(controllerType); 
    } 
} 

internal class WindsorResolver : IDependencyResolver, IDependencyScope, IDisposable 
{ 
    private readonly IWindsorContainer _container; 

    public WindsorResolver(IWindsorContainer container) 
    { 
     this._container = container; 
    } 

    public IDependencyScope BeginScope() 
    { 
     return new WindsorDependencyScope(this._container); 
    } 

    public void Dispose() 
    { 
     this._container.Dispose(); 
    } 

    public object GetService(Type serviceType) 
    { 
     if (!this._container.Kernel.HasComponent(serviceType)) 
      return (object)null; 
     else 
      return this._container.Resolve(serviceType); 
    } 

    public IEnumerable<object> GetServices(Type serviceType) 
    { 
     if (!this._container.Kernel.HasComponent(serviceType)) 
      return (IEnumerable<object>)new object[0]; 
     else 
      return Enumerable.Cast<object>((IEnumerable)this._container.ResolveAll(serviceType)); 
    } 
} 

public class WindsorWebApiControllerActivator : IHttpControllerActivator 
{ 
    private readonly IDependencyResolver _container; 

    public WindsorWebApiControllerActivator(IDependencyResolver container) 
    { 
     _container = container; 
    } 

    public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
    { 
     var scope = _container.BeginScope(); 
     var controller = (IHttpController)scope.GetService(controllerType); 
     request.RegisterForDispose(scope); 
     return controller; 
    } 
} 

//WebApiConfig.cs 
public static class WebApiConfig 
{ 
    public static void Start() 
    { 
     GlobalConfiguration.Configure(WebApiConfig.Register); 
    } 

    public static void Register(HttpConfiguration config) 
    { 
     // initialize and map all attribute routed Web API controllers (note: this does not enable MVC attribute routing) 
     config.MapHttpAttributeRoutes(); 
     config.EnsureInitialized(); 

     //config.Routes.MapHttpRoute(
     // name: "DefaultApi", 
     // routeTemplate: "api/{controller}/{id}", 
     // defaults: new {id = RouteParameter.Optional}); 


     // force JSON responses only (no XML) 
     config.Formatters.Clear(); 
     config.Formatters.Add(new JsonMediaTypeFormatter()); 
    } 
} 

//Global.asax.cs 
public class MvcApplication : Sitecore.Web.Application 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new HandleErrorAttribute()); 
    } 

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

    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 
    } 
} 

如果我添加一個測試ApiController和嘗試去'/ api/Test',它每次都會給我一個404。我用RouteDebugger查看什麼是錯,我得到下面的錯誤每次:

public IEnumerable<string> Get() 
    { 
     return new string[] { "value1", "value2" }; 
    } 


    public string Get(int id) 
    { 
     return "value"; 
    } 

我不知道在哪裏「API/Sitecore的」是從哪裏來的。我遵循了[WebApi2屬性路由與Sitecore] [1]後的說明,但無法使其工作。有人能指點我做錯了什麼嗎?

回答

0

Sitecore使用/api/作爲其默認路由URL。
控制器重命名爲ApiController別的東西或更改Global.asaxweb.config

+0

嘿@Ruud van Falier,謝謝你的回覆。我的控制器被命名爲TestController,它繼承了ApiController。我遵循此鏈接的http://kamsar.net/index.php/2014/05/using-web-api-2-attribute-routing-with-sitecore/說明。但它不適合我。我是否應該在Global.asax.cs中添加自定義路由,以及哪種類型會破壞屬性路由的目的? – Chitrang 2014-10-13 12:33:03

+0

我通知了Kam你的問題,也許他可以幫忙。 – 2014-10-13 18:31:32

+0

嘿@Ruud van Falier,我可以通過攔截這個鏈接[SitecoreWebApi](http://patrickdelancy.com/2013/08/sitecore-webapi-living-harmony/)中提到的管道來獲得WebApi。但現在看來,常規的sitecore路線不起作用。我添加了一個SitecoreController並試圖用「api/sitecore/{controller}/{action}/{id}」來調用它,並且它給了我一個404. – Chitrang 2014-10-15 15:30:03

0

Sitecore的默認路由從看一眼它 看起來像有沒有註冊的路線。 WebApiConfig中唯一的路由被註釋掉了。

+0

嘿@Vasiliy Fomichev,我已經添加了「config.MapHttpAttributeRoutes(); 「它解析了WebApi的屬性路由。儘管如此,通過遵循此鏈接[SitecoreWebApi](http://patrickdelancy.com/2013/08/sitecore-webapi-living-harmony/#.VD6RwRaOpsk)中的步驟,我能夠使其工作。 – Chitrang 2014-10-15 15:32:34