Tag: asp.net web api

validation在unit testing中失败

我正在运行PostMyModel路线的unit testing。 但是,在PostMyModel()我使用了行Validate(model)来更改模型后重新validation我的模型。 我正在使用测试上下文,以便不依赖于unit testing的数据库。 我在下面发布了测试上下文和post方法: 测试环境 class TestAppContext : APIContextInterface { public DbSet MyModel { get; set; } public TestAppContext() { this.MyModels = new TestMyModelDbSet(); } public int SaveChanges(){ return 0; } public void MarkAsModified(Object item) { } public void Dispose() { } } 发布方法 [Route(“”), ResponseType(typeof(MyModel))] public IHttpActionResult PostMyModel(MyModel model) { //Save model […]

运行代码第一次迁移update-database时出错

我无法迁移到我的数据库,我似乎找不到我得到的错误的答案 System.MissingMethodException: Method not found: ‘System.Data.Entity.Migrations.Builders.TableBuilder`1 System.Data.Entity.Migrations.Builders.TableBuilder`1.Index(System.Linq.Expressions.Expression`1>, Boolean, Boolean, System.Object)’. at Evento.Migrations.initialcreate.Up() at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.c__DisplayClassc.b__b() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() […]

如何在Web API授权属性中获取请求cookie?

在.NET中有两个AuthorizeAttribute类。 一个在System.Web.Http命名空间中定义的: namespace System.Web.Http { // Summary: // Specifies the authorization filter that verifies the request’s System.Security.Principal.IPrincipal. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] public class AuthorizeAttribute : AuthorizationFilterAttribute { // Summary: // Initializes a new instance of the System.Web.Http.AuthorizeAttribute class. public AuthorizeAttribute(); // Summary: // Gets or sets the authorized roles. // […]

使用Directory.Delete()和Directory.CreateDirectory()覆盖文件夹

在我的WebApi操作方法中,我想使用以下代码创建/覆盖文件夹: string myDir = “…”; if(Directory.Exists(myDir)) { Directory.Delete(myDir, true); } Directory.CreateDirectory(myDir); // 1 – Check the dir Debug.WriteLine(“Double check if the Dir is created: ” + Directory.Exists(myDir)); // Some other stuff here… // 2 – Check the dir again Debug.WriteLine(“Check again if the Dir still exists: ” + Directory.Exists(myDir)); 问题 奇怪的是,有时在创建目录后,该目录不存在! 有时第一次检查目录时(数字1是); Directory.Exist()返回true ,其他时间返回false 。 […]

从Web APIurl中删除“api”前缀

我有一个API控制器 public class MyController : ApiController { … } 默认情况下,它映射到URL mysite/api/My/Method ,我希望它没有“api”前缀的URL: mysite/My/Method 设置控制器属性[RoutePrefix(“”)]对我没有帮助。 有没有其他方法可以实现这一目标?

安装CORS后无法加载文件

我使用所有默认值在VS Express 2013中创建了一个基本的ASP.NET Web Api应用程序。 我添加了一个控制器,它就像我想要的那样返回XML。 一旦我安装CORS包: Install-Package Microsoft.AspNet.WebApi.Cors -Pre 我甚至不能再运行该应用程序了: An exception of type ‘System.IO.FileLoadException’ occurred in mscorlib.dll and wasn’t handled before a managed/native boundary Additional information: Could not load file or assembly ‘System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception […]

为什么只有在我打开Fiddler时才能在ASP.NET中调用API?

我在我的索引控制器中调用这样的API,一切正常,只有我打开Fiddler。 public ActionResult Index() { Base model = null; var client = new HttpClient(); var task = client.GetAsync( “http://example.api.com/users/john/profile”) .ContinueWith((taskwithresponse) => { var response = taskwithresponse.Result; var readtask = response.Content.ReadAsAsync(); readtask.Wait(); model = readtask.Result; }); task.Wait(); return View(model); } 如果我关闭Fiddler我会收到以下错误: 无法建立连接,因为目标计算机主动拒绝它127.0.0.1:8888 是否有一些我必须包含的配置,以便调用API工作,即使我没有Fiddler打开。

WebAPI令牌颁发授权

我目前正在使用Sessions并重写AuthorizeAttribute来管理由MVC Web应用程序使用的WebAPI端点的授权。 我被告知发行令牌是管理用户和角色的最佳方式。 我想要了解的是: 为什么它比使用会话更好? 有人可以提供一个好的(简单)示例,说明如何在用户使用WebAPI端点登录时发出令牌,以及如何在发出令牌后使用/跟踪令牌。 我一直在研究OWIN和其他一些东西,我很难找到一个如何工作的好例子。

使用OWIN的Websockets

到目前为止我在网络api上使用Microsoft WebSockets的所有示例都使用IIS,实现是在get方法上将HTTP连接升级为websocket,并将websocket处理程序的实例传递给HTTPContext public HttpResponseMessage Get() { if (HttpContext.Current.IsWebSocketRequest) { var noteHandler = new NoteSocketHandler(); HttpContext.Current.AcceptWebSocketRequest(noteHandler); } return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols); } 我想要实现的是在OWIN管道上做同样的事情。 我面临的问题是连接正在升级以使用Websockets,但它没有使用我的websocket处理程序。 我哪里错了? 请建议。 使用OwinContext的控制器(遵循Nancy使用OWIN的示例WebSockets ), public HttpResponseMessage Get() { IOwinContext owinContext = Request.GetOwinContext(); WebSocketAccept acceptToken = owinContext.Get(“websocket.Accept”); if (acceptToken != null) { var requestHeaders = GetValue<IDictionary>(owinContext.Environment, “owin.RequestHeaders”); Dictionary acceptOptions = null; string[] subProtocols; if […]

有没有为WebAPI项目生成C#HTTPClient包装器的方法?

在即将开展的项目中,我们希望使用ASP.NET WebAPI 2向我们的网站和浏览器客户端公开服务function。 由于我们希望尽可能少的“终点”尽可能地让所有呼叫,甚至“内部”从WebAPI Web服务中消耗我们的服务器。 (即不只是直接新建控制器实例) 我正在寻找帮助生成或支持C#“客户端”的东西,它包装HTTPClient并提供强类型的“代理”。 与通过“添加新服务引用”生成代理时的WCF类似。 我已经阅读了其他类似问题的问题(如此处所见),但想要提出一个与MVC或测试问题无关的更直接的问题。