asp.net-web-api2

Correct way to use Serilog with WebApi2

≡放荡痞女 提交于 2020-01-03 18:02:14
问题 I am in search of the correct way to use serilog with aspnet webapi2. As for now I initialize the global Log.Logger property like that : public static void Register(HttpConfiguration config) { Log.Logger = new LoggerConfiguration() .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200")) { IndexFormat = IndexFormat, BufferBaseFilename = outputLogPath, AutoRegisterTemplate = true, AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6, CustomFormatter = new

Web API method return JSON data

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 17:20:24
问题 I am using ASP.net web API 2.0 and would like my method to return the data in JSON format only. Please suggest the code changes for this below method from the API controller class. public async Task<List<Partner>> GetPartnerList() { return await _context.Partners.Take(100).ToListAsync(); } 回答1: You can use the Json<T>(T content) method of the ApiController public async Task<IHttpActionResult> GetPartnerList() { List<Partner> data = await _context.Partners.Take(100).ToListAsync(); return Json

How to do a REST API post request with FileUpload to an Azure AD Protected REST API

筅森魡賤 提交于 2020-01-03 16:45:31
问题 I have the following .net WEB API // [Authorize] public class TenantController : ApiController { public async Task<List<Tenant>> GetTenants() { var tenantStore = CosmosStoreFactory.CreateForEntity<Tenant>(); return await tenantStore.Query().Where(x => x.TenantId != null ).ToListAsync(); } public async Task<IHttpActionResult> GetTenant(string tenantId) { var tenantStore = CosmosStoreFactory.CreateForEntity<Tenant>(); var tenant = await tenantStore.Query().FirstOrDefaultAsync(x => x.TenantId ==

WebAPI call using volley

时光怂恿深爱的人放手 提交于 2020-01-03 11:37:08
问题 I'm trying to get access tokens from the server using a volley String request. I have tried making a JsonObjectRequest also. Both are below. public void getAuthenticationTokens(Object param1, final CustomListener<String> listener) { //String url = prefixURL + "this/request/suffix"; String url = "https://lw.xxx.co.uk/connect/token"; StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { //

Model validation - Why ModelState.IsValid always returns true even when no values are supplied?

点点圈 提交于 2020-01-03 06:19:09
问题 This is regarding WEBAPI and the following is my Model class. public class Request { public int Id { get; set; } public string Name { get; set; } [Required] public Gender Gender { get; set; } } And my controller function (POST) public class Values1Controller : ApiController { public IHttpActionResult Post([FromBody] Models.Request request) { if (!ModelState.IsValid) { return BadRequest(); } var gender = request.Gender; var id = request.Id; var name = request.Name; // do some operations!

is it possible to get an offline version of swagger documentation for a website?

时光毁灭记忆、已成空白 提交于 2020-01-03 03:35:10
问题 localhost/swagger is loading as expected for me but remoteserver/swagger is having issues. Is it possible for me to save an offline copy of the swagger documentation that's generated? I can just send a zip file to a few users while I'm trying to debug the remote issue. 回答1: There are lots of ways to provide API docs to your users if you for some reason cannot host Swagger UI yourself. All suggestions assume you have an OpenAPI (Swagger) definition, that is the YAML/JSON file. If you don't

Exception in HttpControllerDispatcher

戏子无情 提交于 2020-01-03 02:22:54
问题 In my WebApiConfig::Register(...) I replaced the HttpControllerSelector with my own controller selector. When I fire off a POST request the SelectController member is correctly called and I return a ControllerDescriptor with the correct type of my controller. But then HttpControllerDispatcher is raising an exception saying "The given was not present in the dictionary." Anyone has an idea how to debug such error? The complete exception is message is: The given key was not present in the

Azure AD application roles

拥有回忆 提交于 2020-01-02 19:28:08
问题 I'm trying to create a protected controller via Azure AD application roles. Here is an exempt from Startup.Auth, which is basically provided by Visual Studio template: public void ConfigureAuth(IAppBuilder app) { ApplicationDbContext db = new ApplicationDbContext(); app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions

With Azure App Service Easy Auth + Azure AD B2C is it possible to secure a single Web API and have multiple native apps consume it?

泄露秘密 提交于 2020-01-02 12:23:33
问题 We have a Web API intended to serve multiple business partners, each of which will be customizing a white label version of our native app client. We also have a Web API offering common functions to different apps. We would like to use AD B2C as the identity and auth system, but cannot see how or if it is possible to use AD B2C to secure a common API for multiple apps. Is this achievable? 回答1: It depends on how you want to your partners usig the account login-in. If you expected that the

Angular download csv file with Web Api 2

拥有回忆 提交于 2020-01-02 10:00:43
问题 I am trying to download a csv file using web api 2 and angular js. This is my controller code public IHttpActionResult ExportCsvData() { var stream = new FileStream("Testcsv.csv", FileMode.Open, FileAccess.Read); var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(