Tag: asp.net web api

WebApi2属性路由inheritance了控制器

我正在尝试使用基本控制器创建基本的REST API,如下所示: 基类: public abstract class WebApiEntityController : ApiController where TEntity : EntityBase { private readonly IRepository _repository; protected WebApiEntityController(IRepository repository) { _repository = repository; } [Route(“”)] [WebApiUnitOfWork] public HttpResponseMessage Get() { return Request.CreateResponse(HttpStatusCode.OK, _repository.ToList()); } [……….] 派生类: [RoutePrefix(“api/TimesheetTask”)] public class TimesheetTaskController : WebApiEntityController { private readonly IRepository _timeSheetTaskRepository; public TimesheetTaskController(IRepository timeSheetTaskRepository) : base(timeSheetTaskRepository) { […]

webapi与oauth,撤销令牌?

大家好,我在这里按照这篇文章使用owin oauth实现一个带有个人帐户的webapi。 链接 我的问题是有没有办法撤销令牌访问webapi,“退出”方法真的对我没有任何作用,因为通过电话应用程序访问此webapi。 感谢您的任何信息!

凭据标志为“true”,但是“Access-Control-Allow-Credentials”

我正在尝试从AngularJS页面连接到ASP.NET Web-API Web服务,我得到以下内容 凭据标志为“true”,但“Access-Control-Allow-Credentials”标头为“”。 允许凭据必须为“true”。 因此不允许来源’ http:// localhost:221 ‘访问。 var cors = new EnableCorsAttribute(“http://localhost:221”, “*”,”GET,PUT,POST,DELETE”); config.EnableCors(cors); 使用此AngularJS $http({ method: ‘GET’, url: ‘http://localhost:1980/api/investors/62632’, headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’ }, withCredentials: true // withCredentials: true, }).then(function onUserComplete(response) { // this callback will be called asynchronously // when the response is available }, function onError(response) { // called asynchronously […]

在WebAPI中使用ContinueWith进行死锁

作为通过Web API公开一些现有代码的一部分,我们遇到了很多死锁。 我已经能够将这个问题提炼到这个非常简单的例子中,这个例子将永远挂起: public class MyController : ApiController { public Task Get() { var context = TaskScheduler.FromCurrentSynchronizationContext(); return Task.FromResult(1) .ContinueWith(_ => { }, context) .ContinueWith(_ => Ok(DateTime.Now.ToLongTimeString()), context); } } 对我来说,这段代码看起来很简单。 这可能看起来有点人为,但这只是因为我尝试尽可能地简化问题。 看起来像这样链接的两个ContinueWith会导致死锁 – 如果我注释掉第一个ContinueWith(实际上并没有做任何事情),它会正常工作。 我也可以通过不给出特定的调度程序来“修复”它(但这对我们来说不是一个可行的解决方案,因为我们的实际代码需要在正确/原始的线程上)。 在这里,我将两个ContinueWiths放在一起,但在我们的实际应用中,有很多逻辑正在发生,而ContinueWiths最终来自不同的方法。 我知道我可以使用async / await重写这个特定的例子,它会简化一些事情,似乎可以修复死锁。 但是,我们在过去几年中已经编写了大量遗留代码 – 其中大部分都是在异步/等待出现之前编写的,所以它大量使用了ContinueWith。 如果我们能够避免它,重写所有逻辑并不是我们现在想做的事情。 像这样的代码在我们遇到的所有其他场景(桌面应用程序,Silverlight应用程序,命令行应用程序等)中运行良好 – 它只是给我们这些问题的Web API。 有没有什么可以一般地解决这种僵局? 我正在寻找一种解决方案,希望不会重新编写所有的ContinueWith来使用async / await。 更新: 上面的代码是我控制器中的整个代码。 我试图用最少量的代码使这个可重复。 […]

MVC 4 Web Api Post

我想从远程客户端插入,因为我需要通过http发送数据。 我可以正确使用getPerformances()和httpClient api/performances?date={0} 我想问一下我的postPorformances()实现是否正确以及是否如何从客户端调用它? 这是我的实施: public class PerformancesController : ApiController { // GET api/performances public IEnumerable getPerformances(DateTime date) { return DataProvider.Instance.getPerformances(date); } public HttpResponseMessage postPerformances(Performance p) { DataProvider.Instance.insertPerformance(p); var response = Request.CreateResponse(HttpStatusCode.Created, p); return response; } } public class Performance { public int Id {get;set;} public DateTime Date {get;set;} public decimal Value {get;set;} } 我试过这个,但我要注意: […]

用于用户管理的Identity Server和web api

我正在为我的项目使用Identity Server3,我目前有一个网站和api受Id服务器保护,这工作正常但是因为我将用户存储在Id Server数据库中我无法真正更改任何用户的数据从网站上更改个人资料图片或任何索赔值。 为了解决这个问题,我正在考虑在IdServer之上创建API,这个API将管理用户,更改密码,检索用户或更改与用户相关的任何内容,我想在示例项目上创建此API我有我的IdServer使用Owin映射。 现在我在/身份路线中有我的idServer public class Startup { public void Configuration(IAppBuilder app) { Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.Trace().CreateLogger(); app.Map(“/identity”, idserverApp => { var efConfig = new EntityFrameworkServiceOptions { ConnectionString = “IdSvr3Config” }; var options = new IdentityServerOptions { SiteName = “Identity server”, IssuerUri = ConfigurationManager.AppSettings[“idserver:stsIssuerUri”], PublicOrigin = ConfigurationManager.AppSettings[“idserver:stsOrigen”], SigningCertificate = Certificate.Get(), Factory = Factory.Configure(efConfig), AuthenticationOptions […]

尽管更新冲突,但EF不会抛出DbUpdateConcurrencyException

我在Web客户端的Web API服务器上使用EF 6.x(代码优先),我需要实现并发处理。 问题是我甚至无法获得EF来生成exception。 我发现的大多数示例似乎都没有使用“分离实体”,其中DTO被发送到Web客户端,在那里它被更新,然后在以后保存回服务器(这是我的场景)。 假设我有公司记录: public class Company { int CompanyId { get; set; } string CompanyName { get; set; } 1541627290 public byte[] RowVersion { get; set; } } 1)用户A上拉公司, Id = 0, RowVersion为0x0000000000002B0A 2)我运行UPDATE Company SET CompanyName = ‘Acme Changed, Inc.’ WHERE CompanyId = 0 UPDATE Company SET CompanyName = ‘Acme Changed, […]

ODataController返回HTTP 406不可接受

我正在Web API 2.2上构建OData 3服务。 该服务正确返回我的实体的元数据,但在查询其中一个实际实体时返回406 Not Available 。 我做了很多研究(我目前正在学习几个教程),但我还没有找到任何实际工作的东西。 这是我的WebApiConfig: using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using System.Web.OData.Builder; using System.Web.OData.Extensions; namespace MyProject { public static class WebApiConfig { public static void Register(HttpConfiguration config) { ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet(“MarvelCharacters”); config.MapODataServiceRoute( routeName: “Marvel”, routePrefix: “dude”, model: builder.GetEdmModel()); } } } 和我的控制器(不完整,但你明白了): using System; […]

具有特殊字符的ASP.NET MVC标识电子邮件/用户名

通过Web API使用“xxx-yyy@gmail.com”等电子邮件注册帐户时,Fiddler会返回以下错误。 请注意,电子邮件也用于用户名,因此两个字段都相同 。 但它在注册MVC本身时有效。 ExceptionMessage =用户创建失败 – 身份exception。 错误是: 用户名xx-yyy@gmail.com无效,只能包含字母或数字。 用户对象 var newUser = new ApplicationUser { UserName = user.Email, Email = user.Email }; IdentifyConfig.cs public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore(context.Get())); // Configure validation logic for usernames manager.UserValidator = new UserValidator(manager) { RequireUniqueEmail = true, AllowOnlyAlphanumericUserNames […]

Web API读取控制器构造函数中的标头值

是否可以获取Web API控制器的构造函数中的头信息? 我想根据标头值设置变量,但我不想对每个方法都这样做。 我对自定义标头值特别感兴趣,但此时我会考虑授权。 我可以让它在AuthorizationFilterAttribute工作,但我也需要它在控制器级别。 [PolicyAuthorize] public class PoliciesController : ApiController { public PoliciesController() { var x = HttpContext.Current; //will be null in constructor } public HttpResponseMessage Get() { var x = HttpContext.Current; //will be available but too late } } public class PolicyAuthorizeAttribute : AuthorizationFilterAttribute { public override void OnAuthorization(HttpActionContext actionContext) { var authHeader […]