朋友们,我正在使用VS2013,并且在我的应用程序中有路由问题。我创建了一个非常简单的web应用程序来说明我的问题,并将其发布到github:
https://github.com/ewhitmore/RoutingIssues/
目前,我有一个控制器的GET,PUT,POST和DELETE动词,但当我尝试使用它时,GET和POST工作,但删除和放置不。
我收到以下错误:"405方法不允许“和"message":”请求的资源不支持http方法‘PUT’“。
我已经在控制器上放置了断点,PUT/DELETE操作甚至没有到达控制器。
我使用了一个AngularJS ng资源、邮递员和REST控制台,结果是一样的。
--我已经从我的计算机上卸载了webdev,尝试了一系列不同的路由技术,但是没有什么效果。据我所知,这都是本地的,因此不是跨站点/服务器/域(CORS)问题。在这一点上,我已经在这个问题上工作了20多个小时,我即将放弃。任何帮助都将不胜感激。
守则的重点如下:
UserProfileContoller:
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);
}
}
}Web.config重点介绍:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<remove name="FormsAuthenticationModule" />
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Http;
using TrackInstant.App_Start;
namespace TrackInstant
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
WebApiFilterConfig.RegisterGlobalFilters(GlobalConfiguration.Configuration.Filters);
HibernateConfig.InitHibernate();
UnityBootstrapper.Initialise();
}
public override void Dispose()
{
HibernateConfig.SessionFactory.Dispose();
base.Dispose();
}
}
}RouteConfig
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());
}
}
}发布于 2013-12-17 19:33:00
我想明白了!有两个问题。
给自己的注意:多睡一觉,看看那些URL。
我的工作控制器是这样的:
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);
}
}
}https://stackoverflow.com/questions/20638595
复制相似问题