Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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
Fluent nhibernate 流利的Nhibernate+;Web Api v2+;统一_Fluent Nhibernate_Unity Container_Asp.net Web Api Routing - Fatal编程技术网

Fluent nhibernate 流利的Nhibernate+;Web Api v2+;统一

Fluent nhibernate 流利的Nhibernate+;Web Api v2+;统一,fluent-nhibernate,unity-container,asp.net-web-api-routing,Fluent Nhibernate,Unity Container,Asp.net Web Api Routing,朋友们,我正在使用VS2013,在应用程序中遇到路由问题。我创建了一个非常简单的web应用程序来说明我的问题,并将其发布到github: 目前,我有一个带有GET、PUT、POST和DELETE动词的控制器,但当我尝试使用它时,GET和POST工作,而DELETE和PUT不工作 我收到以下错误:“405方法不允许”和“消息”:“请求的资源不支持http方法“PUT”。 我已经在控制器上设置了断点,而put/DELETE操作甚至无法到达控制器 我使用了AngularJSNG资源、Postman和

朋友们,我正在使用VS2013,在应用程序中遇到路由问题。我创建了一个非常简单的web应用程序来说明我的问题,并将其发布到github:

目前,我有一个带有GET、PUT、POST和DELETE动词的控制器,但当我尝试使用它时,GET和POST工作,而DELETE和PUT不工作

我收到以下错误:“405方法不允许”和“消息”:“请求的资源不支持http方法“PUT”。

我已经在控制器上设置了断点,而put/DELETE操作甚至无法到达控制器

我使用了AngularJSNG资源、Postman和REST控制台,得到了相同的结果

我已经从我的计算机上卸载了webdev,尝试了一系列不同的路由技术,但都没有成功。这都是本地的,所以就我所知,不是跨站点/服务器/域(CORS)的问题。在这一点上,我已经在这个问题上工作了20多个小时,我即将放弃。任何帮助都将不胜感激

代码要点如下:

UserProfileController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Http.Description;
using Microsoft.Practices.Unity;
using TrackInstant.Models.Entities;
using TrackInstant.Services;

namespace TrackInstant.Controllers
{
    [AllowAnonymous]
    [RoutePrefix("api/userprofile")]
    public class UserProfileController : ApiController
    {
        [Dependency]
        public UserProfileService UserProfileService { private get; set; }

        [HttpGet]
        public IEnumerable<UserProfile> GetAllUsers()
        {
            return UserProfileService.GetAllUserProfiles();
        }

        [HttpGet]
        [ResponseType(typeof(UserProfile))]
        public UserProfile GetUserById(int id)
        {
            UserProfile userProfile = UserProfileService.GetUser(id);
            if (userProfile == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return userProfile;
        }



        [HttpPost]
        [ResponseType(typeof(UserProfile))]
        public IHttpActionResult PostUser(UserProfile userProfile)
        {

            if (userProfile == null)
            {
                return BadRequest();
            }

            UserProfileService.Save(userProfile);

            return CreatedAtRoute("DefaultApi", new { id = userProfile.UserId }, userProfile);
        }

        [HttpPut]
        public IHttpActionResult PutUser(int id, UserProfile userProfile)
        {
            UserProfile persistentUser = UserProfileService.GetUser(id);
            if (persistentUser == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            persistentUser.Email = userProfile.Email;
            UserProfileService.UpdateUser(persistentUser);

            return StatusCode(HttpStatusCode.NoContent);
        }

        [HttpDelete]
        [ResponseType(typeof(UserProfile))]
        public IHttpActionResult DeleteUser(int id)
        {
            UserProfile userProfile = UserProfileService.GetUser(id);
            if (userProfile == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            UserProfileService.DeleteUser(userProfile);

            return Ok(userProfile);
        }
    }
}
线路图

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls;


namespace TrackInstant
{
    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {

            var settings = new FriendlyUrlSettings {AutoRedirectMode = RedirectMode.Permanent};
            routes.EnableFriendlyUrls(settings);
        }
    }
}
WebApiConfig

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace TrackInstant
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();


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



            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            var formatters = GlobalConfiguration.Configuration.Formatters;

            var jsonFormatter = formatters.JsonFormatter;
            jsonFormatter.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

            var settings = jsonFormatter.SerializerSettings;
            settings.Formatting = Formatting.Indented;
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        }
    }
}
WebApiFilterConfig

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Filters;

namespace TrackInstant.App_Start
{
    public class WebApiFilterConfig
    {
        public static void RegisterGlobalFilters(HttpFilterCollection filters)
        {
            filters.Add(new HibernateSessionFilter());
        }
    }
}

我终于弄明白了!有两个问题

  • 控制器中的PUT动词在声明中包含一个“int id”。这会混淆默认路由,因为在发布过程中url没有包含ID。将PUT声明更改为以下内容修复了该问题:

    公共IHttpActionResult PutUser(用户配置文件用户配置文件)

  • 由于打字错误,我的控制器中的DELETE动词没有从postman或angularjs获得正确的输入

  • 自我提醒:多睡一会儿,看看那些网址

    这是我的工作控制器的外观:

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Description;
    using Microsoft.Ajax.Utilities;
    using Microsoft.Practices.Unity;
    using TrackInstant.Models.Entities;
    using TrackInstant.Services;
    
    namespace TrackInstant.Controllers
    {
    
        public class UserProfileController : ApiController
        {
            [Dependency]
            public UserProfileService UserProfileService { private get; set; }
    
    
            public IEnumerable<UserProfile> GetAllUsers()
            {
                return UserProfileService.GetAllUserProfiles();
            }
    
            public UserProfile GetUserById(int id)
            {
                UserProfile userProfile = UserProfileService.GetUser(id);
                if (userProfile == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return userProfile;
            }
    
            public IHttpActionResult PostUser(UserProfile userProfile)
            {
    
                if (userProfile == null)
                {
                    return BadRequest();
                }
    
                UserProfileService.Save(userProfile);
    
                return CreatedAtRoute("DefaultApi", new { id = userProfile.UserId }, userProfile);
            }
    
    
            public IHttpActionResult PutUser(UserProfile userProfile)
            {
                UserProfile persistentUser = UserProfileService.GetUser(userProfile.UserId);
                if (persistentUser == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                persistentUser.Email = userProfile.Email;
                UserProfileService.UpdateUser(persistentUser);
    
                return StatusCode(HttpStatusCode.NoContent);
            }
    
    
            public IHttpActionResult DeleteUser(int id)
            {
                UserProfile persistentUser = UserProfileService.GetUser(id);
                if (persistentUser == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                UserProfileService.DeleteUser(persistentUser);
    
                return Ok(persistentUser);
            }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    Net系统;
    使用System.Web;
    使用System.Web.Http;
    使用System.Web.Http.Description;
    使用Microsoft.Ajax.Utilities;
    使用Microsoft.Practices.Unity;
    使用TrackInstant.Models.Entities;
    使用TrackInstant.Services;
    命名空间TrackInstant.Controllers
    {
    公共类UserProfileController:ApicController
    {
    [依赖性]
    public UserProfileService UserProfileService{private get;set;}
    公共IEnumerable GetAllUsers()
    {
    返回UserProfileService.GetAllUserProfiles();
    }
    公共用户配置文件GetUserById(int-id)
    {
    UserProfile UserProfile=UserProfileService.GetUser(id);
    if(userProfile==null)
    {
    抛出新的HttpResponseException(HttpStatusCode.NotFound);
    }
    返回userProfile;
    }
    公共IHttpActionResult姿势器(用户配置文件用户配置文件)
    {
    if(userProfile==null)
    {
    返回请求();
    }
    UserProfileService.Save(userProfile);
    返回CreatedAtRoute(“DefaultApi”,新的{id=userProfile.UserId},userProfile);
    }
    公共IHttpActionResult PutUser(用户配置文件用户配置文件)
    {
    UserProfile persistentUser=UserProfileService.GetUser(UserProfile.UserId);
    if(persistentUser==null)
    {
    抛出新的HttpResponseException(HttpStatusCode.NotFound);
    }
    persistentUser.Email=userProfile.Email;
    UserProfileService.UpdateUser(persistentUser);
    返回状态码(HttpStatusCode.NoContent);
    }
    公共IHttpActionResult DeleteUser(内部id)
    {
    UserProfile persistentUser=UserProfileService.GetUser(id);
    if(persistentUser==null)
    {
    抛出新的HttpResponseException(HttpStatusCode.NotFound);
    }
    UserProfileService.DeleteUser(persistentUser);
    返回Ok(persistentUser);
    }
    }
    }
    
    这是put的邮递员输出。注意“允许”:
    Allow→获取,后缓存控制→无缓存内容长度→79内容类型→应用程序/json;字符集=utf-8日期→2013年12月17日星期二16:09:28 GMT到期→-1布拉格马→无缓存服务器→Microsoft IIS/8.0 X-AspNet-Version→4.0.30319 X供电→ASP.NET X源文件→=?UTF-8?B?QZPCVxNLCNNCD2HPDG0xZWTCRG9JDW1LBNRZXFZPC3VHBCBTDHVKAW8GMJAXM1XQCM9QZWN0C1XSB3V0AW5NxFJVDXRPBMDJC3N1ZXNCVHJHY2TJBNN0YW50XGFWAVX1C2VYCHJVZMLSQ==?=
    我认为如果安装了WebDAV,可能会导致这个问题。在发布之前,我从我的计算机上完全删除了WebDAV。还是不走运。我不认为这是一个“路由”配置问题,因为通用控制器工作得很好。我可以点击我设置的断点,而使用userprofile控制器,它甚至没有到达断点。[连结]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http.Filters;
    
    namespace TrackInstant.App_Start
    {
        public class WebApiFilterConfig
        {
            public static void RegisterGlobalFilters(HttpFilterCollection filters)
            {
                filters.Add(new HibernateSessionFilter());
            }
        }
    }
    
        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Description;
    using Microsoft.Ajax.Utilities;
    using Microsoft.Practices.Unity;
    using TrackInstant.Models.Entities;
    using TrackInstant.Services;
    
    namespace TrackInstant.Controllers
    {
    
        public class UserProfileController : ApiController
        {
            [Dependency]
            public UserProfileService UserProfileService { private get; set; }
    
    
            public IEnumerable<UserProfile> GetAllUsers()
            {
                return UserProfileService.GetAllUserProfiles();
            }
    
            public UserProfile GetUserById(int id)
            {
                UserProfile userProfile = UserProfileService.GetUser(id);
                if (userProfile == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return userProfile;
            }
    
            public IHttpActionResult PostUser(UserProfile userProfile)
            {
    
                if (userProfile == null)
                {
                    return BadRequest();
                }
    
                UserProfileService.Save(userProfile);
    
                return CreatedAtRoute("DefaultApi", new { id = userProfile.UserId }, userProfile);
            }
    
    
            public IHttpActionResult PutUser(UserProfile userProfile)
            {
                UserProfile persistentUser = UserProfileService.GetUser(userProfile.UserId);
                if (persistentUser == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                persistentUser.Email = userProfile.Email;
                UserProfileService.UpdateUser(persistentUser);
    
                return StatusCode(HttpStatusCode.NoContent);
            }
    
    
            public IHttpActionResult DeleteUser(int id)
            {
                UserProfile persistentUser = UserProfileService.GetUser(id);
                if (persistentUser == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                UserProfileService.DeleteUser(persistentUser);
    
                return Ok(persistentUser);
            }
        }
    }