Tag: asp.net web api

在WebAPI中定义两个get函数

当我试图在MVC WebAPI中调用GET函数时,我将遵循exception {“$id”:”1″,”Message”:”An error has occurred.”, “ExceptionMessage”:”Multiple actions were found that match the request: \r\nSystem.Xml.XmlNode Get(Int32, System.String) 我认为问题是因为两个get函数我定义了两个函数: 一: [HttpGet] public XmlNode Get(int id, string Tokken) { //Do something } 第二个 [HttpGet] public List GetUsersInteractions(int? activityID, string Tokken) { // Do Something } 路线配置 config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional […]

WebAPI Selfhost:无法将多个参数绑定到请求的内容

下面的代码被简化以显示必要性。 我可以知道出了什么问题吗? 我似乎无法使用[FromBody]属性检索两个参数(在本例中为A和B)。 错误消息是“无法将多个参数(’A’和’B’)绑定到请求的内容” 如果我只有A或B,那就完全没问题了。 Web API: [Route(“API/Test”), HttpPost] public IHttpActionResult Test([FromBody] int A, [FromBody] int B) 客户: HttpClient client = new HttpClient(); var content = new FormUrlEncodedContent( new Dictionary { { “A”, “123” }, { “B”, “456” } }); client.PostAsync(“http://localhost/API/Test”, content).Result;

在Web Api 2中启用会话

我知道REST应该是无国籍的。 我的Web Api与我的MVC网站属于同一个项目。 我如何分享他们之间的会话? 我正在尝试使用Web Api 2的好东西并使用Ajax而不是构建RESTful API。

资源不会加载当前的文化

我试图改变ASP.NET Web API 2上的文化,但我无法弄清楚要覆盖哪个函数 我尝试过ExecuteAsync和Initialize但是应用程序使用英语资源无论如何。 这就是我设定文化的方式 protected void SetCulture(string cultureName) { // Validate culture name cultureName = CultureHelper.GetImplementedCulture(cultureName); // Modify current thread’s cultures Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; } protected override void Initialize(HttpControllerContext controllerContext) { if (controllerContext.Request.Headers.AcceptLanguage != null && controllerContext.Request.Headers.AcceptLanguage.Count > 0) { string language = controllerContext.Request.Headers.AcceptLanguage.First().Value; SetCulture(language); } base.Initialize(controllerContext); } 这里的资源总是英文。 […]

在WebApi服务中validationAzure移动服务令牌

我正在尝试在使用WebApi服务的跨平台移动应用程序上实现身份validation和身份validation。 我的计划是将身份validation导出到联合云服务,例如新的Azure移动服务。 客户端移动应用程序将使用移动服务身份validation流程,获取令牌,而不是将请求的头部发送到WebApi,然后WebApi将对其进行validation并从中提取UserId。 假设我已经使用DelegatingHandler拦截器配置了WebApivalidationJWT令牌,是否可以validationAzure移动服务发布的令牌? SymmetricKey,Issuer和Audience的正确值是什么? 我正朝着正确的方向前进吗?

Web API响应时间

我正在构建一个服务器监视系统,并希望向Web API发送请求,并在JSON对象中获取服务器的运行状况,如果没有问题,如果数据库连接正常,响应时间等等。 如何实现响应时间,说明Web API响应请求需要多长时间?

自定义表单数据与多个文件到Web API控制器

我在获取我在API控制器中指定的所有表单数据时遇到问题。 javascript上传function: $scope.upload[index] = $upload.upload({ url: ‘/api/upload/’, method: ‘POST’, data: { Photographer: $scope.models[index].photographer, Description: $scope.models[index].desc }, file: $scope.models[index].file }) 表单数据:按照我的意愿工作,对于发送的每个请求,它包含我想要的值。 ——WebKitFormBoundaryzlLjAnm449nw1EvC Content-Disposition: form-data; name=”Photographer” Scott Johnson ——WebKitFormBoundaryzlLjAnm449nw1EvC Content-Disposition: form-data; name=”Description” Image taken with a Nikon camerea ——WebKitFormBoundaryzlLjAnm449nw1EvC Content-Disposition: form-data; name=”file”; filename=”moxnes.jpg” Content-Type: image/jpeg 我的Web API控制器: 本指南中的模板 public class UploadController : ApiController { public async Task […]

将Aurelia的数据和文件发布到ASP.NET webapi

我正在尝试将带有文件上传的输入添加到我的应用程序中。 这是我的视图,有两个输入,一个文本和一个文件: Language key Upload file Do import 在我的webapi中,我有这个代码,我从这里复制并粘贴: public class ImportLanguageController : ApiController { public async Task Post() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath(“~/App_Data”); var provider = new MultipartFormDataStreamProvider(root); try { // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); // This illustrates […]

WebApi 2.0路由与查询参数不匹配?

我刚刚从AttributeRouting切换到WebApi 2.0 AttributeRouting,并且有一个控制器和动作定义如下: public class InvitesController : ApiController { [Route(“~/api/invites/{email}”)] [HttpGet] [ResponseType(typeof(string))] public IHttpActionResult InviteByEmail(string email) { return this.Ok(string.Empty); } } 示例查询: GET: http://localhost/api/invites/test@foo.com 我收到的响应是200,内容为空(由于string.Empty)。 这一切都很好 – 但我想将电子邮件属性更改为查询参数。 所以我将控制器更新为: public class InvitesController : ApiController { [Route(“~/api/invites”)] [HttpGet] [ResponseType(typeof(string))] public IHttpActionResult InviteByEmail(string email) { return this.Ok(string.Empty); } } 但现在在查询端点时: GET: http://localhost/api/invites?email=test@foo.com 我收到的回复是404: { “message”: “No HTTP […]

从MVC的DependencyResolver过渡到AutofacWebApiDependencyResolver – 哪里是.Current?

我让AutoFac与MVC4一起正常工作。 我正在尝试过渡到Web API 2.这是我设置AutoFac所得到的: public class AutofacRegistrations { public static void RegisterAndSetResolver() { // Create the container builder. var containerBuilder = new ContainerBuilder(); // Register the Web API controllers. containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly()); // Only generate one SessionFactory ever because it is expensive. containerBuilder.Register(x => new NHibernateConfiguration().Configure().BuildSessionFactory()).SingleInstance(); // Everything else wants an instance of Session per HTTP request, […]