asp.net-web-api2

405 Method Not Allowed on WebAPI2 (OWIN)

允我心安 提交于 2019-12-02 19:15:16
I am setting up a WebAPI endpoint for my API, but am having trouble getting my AngularJS calls to my PostRegister method to work. The webAPI and Angular are on separate sites. On the Startup.cs I have: public void Configuration(IAppBuilder app) { ConfigureOAuth(app); var config = new HttpConfiguration(); WebApiConfig.Register(config); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); app.UseWebApi(config); } private void ConfigureOAuth(IAppBuilder app) { var oAuthServerOptions = new OAuthAuthorizationServerOptions { AllowInsecureHttp = true, TokenEndpointPath = new PathString(Paths

Multiple parameters in a web api 2 get

核能气质少年 提交于 2019-12-02 19:09:10
I want to make a web api that is passed 4 parameters. Here is my route: config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{email}/{firstname}/{lastname}/{source}" ); Here is the method signature public string GetId(string email, string firstname, string lastname, string source) Here is the calling url http://fakedomain.com/api/Contacts/GetId?email=user@domain.com&firstname=joe&lastname=shmoe&source=123 I get a 404 error. If I set each parameter to optional in the route config, and set up each argument with a default value it gets called. However, each argument

Return an error when invalid parameters are specified in ASP Web API

我们两清 提交于 2019-12-02 18:39:33
问题 I'm creating an API using C# and ASP.NET Web API and I want it to return an error when a parameter is used that isn't recognised. For example: /api/Events should a list of events /api/Events?startTime={{startTime}} should return a list of events that started at a particular time /api/Events?someRandomInvalidParameter={{something}} should return an error Is there a nice config way to do this? If not, how can I get a list of parameters to check myself. 回答1: You could create an ActionFilter to

ASP.NET Web API generate url using Url.Action

[亡魂溺海] 提交于 2019-12-02 17:55:18
How can I generate the same url but in Web Api ? var url = Url.Action("Action", "Controller", new { product = product.Id, price = price }, protocol: Request.Url.Scheme); P.S. The url should be generated to an MVC controller/action but from within web api. So basically: make a get request to my api/generateurl and that will return an url to : http://domain.com/controller/action?product=productId&price=100 Viktor Bahtev Maybe the closest helper to Url.Action in Web Api Controller is the Url.Link method which will generate the url by Route name, Controller Name, Action Name and the route

Autofac with Owin

强颜欢笑 提交于 2019-12-02 16:18:26
问题 I have an issue with Autofac . The documentation clearly states that when using Web API 2 and OWIN you must not use GlobalConfiguration.Configuration anywhere: A common error in OWIN integration is use of the GlobalConfiguration.Configuration. In OWIN you create the configuration from scratch. You should not reference GlobalConfiguration.Configuration anywhere when using the OWIN integration. which can be found here (at the bottom of the page): http://autofac.readthedocs.io/en/latest

Web API 2 : Best Practice returning ViewModel data from Controller to $http Success result ?

家住魔仙堡 提交于 2019-12-02 14:13:38
I wonder what's the recommended way to handle data (ViewModels) in Web Api 2.. I have googled around quite much, and found some "recepies" but i wonder what's the most flexible and easy way of handling this.. This call returns error --> GET http://localhost:63203/api/Gallery/GetPhotosForPage 404 (Not Found) probably because of some signature error..,. Here is the $http call (Angular) : var currPage = $location.path() || "Unknown"; $http({ method: 'GET', url: '/api/Gallery/GetPhotosForPage', accept: 'application/json', data: JSON.stringify(currPage) //currpage = ex. "/leftwing" }) .success

get 404 when trying to access files directly [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-02 10:27:31
This question is an exact duplicate of: HTML page with images using .net WebApi 2 answers I have a WebApi project with a folder called "images". I want to access the images directly but I keep getting 404 with message "No type was found that matches the controller named 'images'." I did the same thing on MVC project and it always worked well, I don't know why in WebApi I can't access files. I'm sure the file is there, I see it in the directory. Example request: GET http://localhost:49873/images/20180704003126241.png Under a default configuration WebAPI won't serve static files for you. You

How to download memory stream object via angularJS and webaAPI2

主宰稳场 提交于 2019-12-02 10:22:13
问题 I have a project where I am using NPOI to generate Excel document from my Angular application. I have the calls being made from my angular service to my webapi controller as follows: function exportReportToExcel(report) { return $http.post('reportlibrary/exportReport/', report, { }).then(function (response) { return response.data; }); }; Within the controller I make the following call [HttpPost] public HttpResponseMessage ExportReport([FromBody]DTOs.Report report) { try {

How to post and receive a file with web api

久未见 提交于 2019-12-02 10:05:52
I have a Api Post method that I want to be able to accept any file type and that looks like this: [HttpPost] public async Task<IHttpActionResult> Post() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new MultipartMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider); if (provider.Contents.Count != 1) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "You must include exactly one file per request.")); } var file = provider.Contents[0]; var filename

asp.net webapi 2 post parameter is always null

吃可爱长大的小学妹 提交于 2019-12-02 09:06:31
问题 I have been trying to figure this our for the last hour, but I can't see what's wrong with it the post is from a xamarin app that I'm writing, using restsharp portable client POST http://192.168.30.103:8080/api/Order HTTP/1.1 Authorization: Basic xxxx Content-Length: 51 Content-Type: application/json; charset=utf-8 Host: 192.168.30.103:8080 Connection: Keep-Alive Pragma: no-cache {"CheckinCount":1,"EventId":16630,"OrderNo":470292} It hits the server Post API correctly, but the parameter is