当我在WebAPI控制器中有一个返回类型’string’时,SuccessStatusCode在我的MVC控制器中返回’OK’,但是当返回类型是名为’USER’的模型时,我得到这个内部服务器错误.这是我的代码: 的WebAPI:public class UserController : ApiController {OnlineCenterEntities db = new OnlineCenterEntities();public USER GetUserInfo(string userName, string domain){USER userInfo = (from u in db.USERswhere u.USER_NAME.ToUpper() == u...
我有Web api控制器Uploads Controller,它有PostUpload方法将数据存储到数据库. 现在我试图将文件和一些参数发布到那个web api但是所有的尝试都失败了,就像传递数组列表,json对象一样,我们不能将文件和参数发布到web api?var request = new RestRequest("Uploads", Method.POST); request.RequestFormat = DataFormat.Json;request.AddHeader("Content-Type", "application/json"); request.AddFile("filename", Server.MapPath("/...
好吧,读这篇文章的人可能都知道(我也是如此)WebAPI如何工作以及如何使用WebAPI和Identity框架构建应用程序我可以构建一个http请求,添加一个auth标头,应用程序将通过阅读auth知道我是谁头. 这就是所谓的“无状态”API调用,其中接收调用的API的所有内容都被赋予了确定用户身份所需的所有内容,因此可以对用户进行身份验证并对其请求进行“无状态”操作. ….. 我想在MVC(而不是Web API)中使用这种完全相同的行为. 我希望以前没有对此应...
我创建了一个“测试”项目,我正在使用.Net 4.6 WebApi,我希望使用ADFS集成身份验证 – 类似于this post.我从一个角度项目调用api并使用以下代码我是能够获得授权标题:string authority = ConfigurationManager.AppSettings["adfsEndpoint"].ToString();string resourceURI = "https://localhost:44388/";string clientID = "someguid";string clientReturnURI = "http://localhost:55695/";var ac = new AuthenticationContext(au...
我发现应用程序可以在这个答案中分配给用户,但似乎无法弄清楚如何使用C#来做到这一点.下面是我正在尝试的JSON和C#. Azure Active Directory: assign user to an application from the gallery via Graph APIAssign principal (user or group) to application: ?resourceId is the objectId of the servicePrincipal that get created in the tenant for the application ?id is the default role id of App. ?principalId is the o...
当我打电话给Paypal Rest API时,我的RestSharp组件出错了. 我有以下代码使用Xamarin for Android.public async Task<PayPalGetTokenResponse> GetAccessToken(){var restRequest = new RestRequest("/oauth2/token", Method.POST);// Add headersrestRequest.AddHeader("Accept", "application/json");restRequest.AddHeader("Accept-Language", "en_US");// Make Authorization headerrestClient.Authenticator = new HttpBasicAu...
我正在为创建一个具有流畅API的小型SQL库而烦恼,并希望做到这样的事情:var person = connection.GetOne<Person>("select * from [Person] where [Id] = 1").WithMany<Pet>("select * from [Pet] where [PersonId] = 1").WithMany<Address>("select * from [Address] where [PersonId] = 1]").Build((person, pets, addresses) =>{person.Pets = pets;person.Addresses = addresses;return person;});我之前已经构建了大量流畅的AP...
我需要在’Web API控制器操作’中创建一个复杂的类型参数,这样我的空值操作过滤器可以通过检查参数的属性IsOptional来忽略它.我收到这个错误:Optional parameter ‘errorCode’ is not supported by ‘FormatterParameterBinding’.我有一个像这样的控制器:[Route("api/v1/Temp/{number}/{errorCode?}/{value?}")] [HttpGet] public IHttpActionResult Temp(int number, ErrorDetail errorCode = null, string value = null) {re...
我想为ApiController创建扩展方法,以便能够返回自定义内容.我的想法是用我自己的细节返回自定义错误.我想返回类似于OAuthAuthorizationServerProvider返回的错误的自定义错误:{"error": "invalid_grant","error_description": "You have 3 more attempts before Your account will be locked." }在我的ApiController里面我添加了这个方法:public IHttpActionResult Test() {HttpError err = new HttpError();err["error"] = "400...
下面是WebAPI.[RoutePrefix("api/customer")] public class CustomerController : ApiController {[Route("{id:int:min(1)}/")]public HttpResponseMessage Get(int id){//my stuff} }如果我传递任何小于1的值(比如说0或-1).它返回响应体为NUll,HttpStatusCode = 200 预期的响应是:HttpStatus Code = 404. 但是,如果我修改我的路线如下.[RoutePrefix("api/customer")] public class CustomerController : ApiController {[Route("...
我正在尝试使用他们的upload api将我的本地json文件上传到mapbox.我遵循以下步骤: >检索S3凭据以暂存文件>使用S3客户端(如AWS SDK)使用这些凭据将文件上载到S3>使用暂存文件的URL创建上传>在上传处理到tileset时检索上载的状态>上传完成后,使用tileset ID,就像使用任何其他tileset一样. 我完成了第1步和第2步,但在第3步中遇到以下错误:The remote server returned an error: (422) Unprocessable Entity.以下是我的代码(步骤1):...
我们可以通过以下方式从web api返回带有BadRequest的ModelState:return BadRequest(ModelState);它提供以下输出:{"Message": "The request is invalid.","ModelState": {"property": ["error"]} }如何以Forbidden状态返回相同的输出? 我试过以下方法:return Content(HttpStatusCode.Forbidden, ModelState);但它返回:{"property": {"_errors": [{"<Exception>k__BackingField": null,"<ErrorMessage>k__BackingField": "error...
流媒体视频有问题.我在ASP.NET Web API 2上开发了服务器并实现了两种方法: 第一种方法:if (Request.Headers.Range != null){try{var httpResponce = Request.CreateResponse();httpResponce.Content =new PushStreamContent((Action<Stream, HttpContent, TransportContext>) WriteContentToStream);return httpResponce;}catch (Exception ex){return new HttpResponseMessage(HttpStatusCode.InternalServerError);}}else{retu...
我有使用ASP.NET Web API 2.0的项目,并在此API中有一个方法抛出异常:public void TestMethod(){throw new Exception("Error40001");}当抛出这个异常时,我已经设置了一个处理这些东西的处理程序:public class APIExceptionHandler : ExceptionHandler {public override void Handle(ExceptionHandlerContext context){var rm = Language.Error.ResourceManager;string message = rm.GetString(context.Exception.Message);string ...
我需要创建一个Serializer来支持以下所有任务: >删除空属性>删除空列表 我注意到ODataMediaTypeFormatter的语法已被更改. 而我在将序列化提供程序添加到管道时遇到问题. 这是我尝试过的: 在WebApiConfig.cs上:var odataFormatters = ODataMediaTypeFormatters.Create(); odataFormatters.Add(new MyDataMediaTypeFormatter()); config.Formatters.InsertRange(0, odataFormatters);另外,我创建了以下Odatameditatypeformatter:...