asp.net-web-api2

How to access a child object using OData, WebAPI and a DTO?

浪尽此生 提交于 2019-12-22 00:28:44
问题 From a WebAPI controller, I am returning a list of ProjectEditorDTO objects from an OData request /odata/ProjectEditor?$format=json&$inlinecount=allpages&$top=10 : [Queryable] public virtual IHttpActionResult Get(ODataQueryOptions<Website> odataQueryOptions) { var userId = UserContext.Identity.UserId; try { var results = odataQueryOptions.ApplyTo(_uow.Repository<Website>() .Query() .Get() .Where(u => u.UserId == userId) .OrderBy(o => o.WebsiteName)).Cast<Website>() .Select(s => new

Implementing Forms authentication over async requests

三世轮回 提交于 2019-12-21 21:14:08
问题 In an angular app with a .net web api backend, I'm trying to implement forms authentication over async requests. Here is the relevant portion of my web.config... <authentication mode="Forms"> <forms loginUrl="/" cookieless="UseCookies" name=".TIMETRACK" requireSSL="false" timeout="30" protection="All" path="/TimeTrack" /> </authentication> Here is my web api login method... [Route("Login")] public HttpResponseMessage Post(AppUser credentials) { var userTemplate = _authenticationProvider

Asp.net Identity, Generate WebApi token OAuthGrantResourceOwnerCredentialsContext - no access to UserManager using Unity

给你一囗甜甜゛ 提交于 2019-12-21 20:09:29
问题 I am trying to setup a project structure so that I have a WebApi, WebUI and Domain layer. I have moved all the Asp.Net.Identity objects into the Domain layer and have also setup the ApplicationContext here too (inheriting from IdentityContext). (I have used this tutorial and package as a base which is excellent. http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/) In the WebAPI layer I am able to use the Account controller correctly to login and register. However

'The parameters dictionary contains a null entry' error, Web API

♀尐吖头ヾ 提交于 2019-12-21 19:27:32
问题 On my Web API I have a Document Controller with two simple actions: [AllowAnonymous] public class DocumentController : ApiController { public String Get(int id) { return "test"; } public String Get(string name) { return "test2"; } } The following URL (executes the first function) works fine: http://localhost:1895/API/Document/5 But this URL (should execute the second function): http://localhost:1895/API/Document/test Throws this error: { "message": "The request is invalid.", "messageDetail":

CORS enabled Web API fails to add header

…衆ロ難τιáo~ 提交于 2019-12-21 12:19:49
问题 I am using the DynamicPolicyProviderFactory as described here. Below is my version of the DynamicPolicyProviderFactory: public class DynamicPolicyProviderFactory : ICorsPolicyProviderFactory { private readonly HashSet<Regex> _allowed; public DynamicPolicyProviderFactory(IEnumerable allowedOrigins) { _allowed = new HashSet<Regex>(); foreach (string pattern in allowedOrigins.Cast<string>() .Select(Regex.Escape) .Select(pattern => pattern.Replace("*", "w*"))) { _allowed.Add(new Regex(pattern,

Generic Web Api method

南笙酒味 提交于 2019-12-21 12:10:47
问题 I've some classes like CustomerModel or CustomerDetailsModel which are inherting from ModelBase . Also i don't want to introduce subclasses for each modeltype. In one case have a post method foreach. So i could manually create multiple routes to call a method which looks like Handle<T>(T model) where T : ModelBase They only differ in the path the were called. For example: baseurl/api/customer => CustomerModel baseurl/api/customerdetails => CustomerDetailsModel I want to implement a generic

Response to preflight request doesn't pass access control check (Angular2)

梦想的初衷 提交于 2019-12-21 08:15:55
问题 I am getting below error on call to REST Web API in Asp.net. XMLHttpRequest cannot load http://localhost:54859/api/PostData. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. I am using Angular2 as Front end. In the back end, I have added following codes to enable CORS in WEB API. var corsAttr = new EnableCorsAttribute("*", "*", "*");

Route all Web API requests to one controller method

吃可爱长大的小学妹 提交于 2019-12-21 07:34:17
问题 Is it possible to customize ASP.NET Web API's routing mechanism to route all requests to the API to one controller method? If a request comes in to www.mysite.com/api/products/ or www.mysite.com/api/otherResource/7 All would be routed to my SuperDuperController's Get() method? 回答1: I ran into a case where I needed to do this. (Web API 2) I first looked into creating custom IHttpControllerSelector and IHttpActionSelector s. However, that was a bit of a murky way around. So I finally settled on

Validate model on specific string values

為{幸葍}努か 提交于 2019-12-21 07:09:32
问题 I'm working on a Web API 2 project. besides the requirement that some properties are required, some only can have specific values. One option is that I could try to save the model to the database (EF6) and create some logic while saving, but I think it is better to validate if the correct value is set before I make a call to the database. Does data annotations provide an attribute like Range but then for specific string values like in the example below? Or do I have to write my own validator

How to remove schema node in web api if return format is xml?

你离开我真会死。 提交于 2019-12-21 04:55:23
问题 I have a web api method that takes format as a parameter that provides returning both xml and json.The data type that method return is DataTable.In json format everything looks fine but in xml format the schema of datatable and some other attributes in xml nodes also returning.How to return simple xml that includes only data of datatable?Also, I am using QueryStringMapping in WebApiConfig. This is WebApiConfig Code public static void Register(HttpConfiguration config) { config