asp.net-core-webapi

Is services.AddSingleton<IConfiguration> really needed in .net core 2 API

岁酱吖の 提交于 2019-12-05 15:19:21
I accessed appsettings.json In .NET Core 2 Web API Controller simply by adding below: public class MyController : Controller { private readonly IConfiguration appConfig; public MyController(IConfiguration configuration) { appConfig = configuration; } } Without adding below in Startup class ConfigureServices(IServiceCollection services) after services.AddMvc();: services.AddSingleton<IConfiguration>(Configuration); Is there any flaws in my approach? In official docs for .Net Core 2 configuration section, its not mentioned to use 'AddSingleton' not even once: https://docs.microsoft.com/en-us

Visual Studio keeps adding IIS Express back into my launchsettings.json

↘锁芯ラ 提交于 2019-12-05 14:51:54
问题 I am trying to remove the IIS Express profile from my .NET Core launch settings but every time i repoen the solution, Visual Studio adds it back in again. For example, in a new project my launch settings looks like this { "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:55735/", "sslPort": 0 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": {

Route Name for HttpGet attribute Name for base generic controller class in asp.net core 2

依然范特西╮ 提交于 2019-12-05 12:45:36
问题 I have a generic controller, which have several derived controller classes. but I cannot figure out how to handle the HttpGet's route name since it require constant. [HttpGet("{id}", Name ="should not hard coded here for derived class")] public virtual async Task<IActionResult> Get(int id) I need the route name because in my HttpPost function I want to return CreatedAtRoute() which require HttpGet's route name The route name cannot be hard coded because all the derived class need to have a

Asp.net core 2.0 middleware - accessing config settings

﹥>﹥吖頭↗ 提交于 2019-12-05 11:51:42
In a asp.net core web api middleware, is it possible to access the configuration inside the middleware? Can someone guide me how this is done? In a middle-ware you can access settings. To achieve this, you need to get IOptions<AppSettings> in the middle-ware constructor. See following sample. public static class HelloWorldMiddlewareExtensions { public static IApplicationBuilder UseHelloWorld( this IApplicationBuilder builder) { return builder.UseMiddleware<HelloWorldMiddleware>(); } } public class HelloWorldMiddleware { private readonly RequestDelegate _next; private readonly AppSettings

Pass multiple parameters in a POST API without using a DTO class in .Net Core MVC

心已入冬 提交于 2019-12-05 11:15:26
I have an action on my web project which calls to an API [HttpPost] public async Task<IActionResult> ExpireSurvey(int id) { var token = await HttpContext.GetTokenAsync("access_token"); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); var path = "/api/forms/ExpireSurvey"; var url = Domain + path; var data = JsonConvert.SerializeObject(id); HttpContent httpContent = new StringContent(data, Encoding.UTF8, "application/json"); var response = await client.PutAsync(url, httpContent); return

ExecuteAsyncPost Example in RestSharp.NetCore

旧时模样 提交于 2019-12-05 09:25:18
I'm working with RestSharp.NetCore package and have a need to call the ExecuteAsyncPost method. I'm struggling with the understanding the callback parameter. var client = new RestClient("url"); request.AddParameter("application/json", "{myobject}", ParameterType.RequestBody); client.ExecuteAsyncPost(request,**callback**, "POST"); The callback is of type Action<IRestResponse,RestRequestAsyncHandler> Would someone please post a small code example showing how to use the callback parameter with an explanation. Thanks -C This worked for me using ExecuteAsync for a Get call. It should hopefully

How to do simple header authorization in .net core 2.0?

无人久伴 提交于 2019-12-05 09:15:25
I have been unable to find information on this particular issue after the 2.0 changes to .NET Core. I have cookie authorization like this: services.AddAuthentication("ExampleCookieAuthenticationScheme") .AddCookie("ExampleCookieAuthenticationScheme", options => { options.AccessDeniedPath = "/Account/Forbidden/"; options.LoginPath = "/Account/Login/"; }); For another part (of my controllers I would like to simply authorize based on a simple header. In the examples I've found, either I am unable to get the headers, or they have been made only for facebook, google, cookies etc. How do I add an

Disable AutoRedirect in FlurlClient

不想你离开。 提交于 2019-12-05 08:58:41
I am using FlurlHttp and I want to disable AllowAutoRedirect for some API calls. I know How can I get System.Net.Http.HttpClient to not follow 302 redirects? WebRequestHandler webRequestHandler = new WebRequestHandler(); webRequestHandler.AllowAutoRedirect = false; HttpClient httpClient = new HttpClient(webRequestHandler); // Send a request using GetAsync or PostAsync Task<HttpResponseMessage> response = httpClient.GetAsync("http://www.google.com") But for Flurl I found only the way similar to described in C# Flurl - Add WebRequestHandler to FlurlClient (I haven't compiled yet the code below ,

ASP.NET Core ways to handle custom response/output format in Web API

孤者浪人 提交于 2019-12-05 08:25:26
I'd like to create custom JSON format, that would wrap the response in data and would return Content-Type like vnd.myapi+json Currently I have created like a wrapper classes that I return in my controllers but it would be nicer if that could be handled under the hood: public class ApiResult<TValue> { [JsonProperty("data")] public TValue Value { get; set; } [JsonExtensionData] public Dictionary<string, object> Metadata { get; } = new Dictionary<string, object>(); public ApiResult(TValue value) { Value = value; } } [HttpGet("{id}")] public async Task<ActionResult<ApiResult<Bike>>> GetByIdAsync

Can you set request timeout in asp.net core 2.0 hosted in IIS from C# code?

喜你入骨 提交于 2019-12-05 08:09:43
Is there a way to set requestTimeout from C# instead of needing to set requestTimeout in the web.config ? asp.net core 2.0 hosted in IIS <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore requestTimeout="00:00:04" processPath="dotnet" arguments=".\Foo.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" /> </system.webServer> </configuration> Not like this No, there is no way to do that as you described. But according to the