asp.net-core-1.0

Alternative to Server.Transfer in ASP.NET Core

时间秒杀一切 提交于 2019-12-04 21:19:28
问题 I am migrating an ASP.NET application to ASP.NET Core and they have some calls to HttpServerUtility.Transfer(string path). However, HttpServerUtility does not exist in ASP.NET Core. Is there an alternative that I can use? Or is Response.Redirect the only option I have? I want to maintain the same behaviour as the old application as much as possible since there is a difference in between Server.Transfer and Response.Redirect. 回答1: I see some options for you, depending on your case: Returning

Custom 404 response model

蹲街弑〆低调 提交于 2019-12-04 20:30:48
I want to provide a custom reponse for all 404s on our API. For example: { "message": "The requested resource does not exist. Please visit our documentation.." } I believe the following result filter works for all cases within the MVC pipeline: public class NotFoundResultFilter : ResultFilterAttribute { public override void OnResultExecuting(ResultExecutingContext context) { var result = context.Result as NotFoundResult; if (result != null) { context.Result = new HttpNotFoundResult(); // My custom 404 result object } } } But, when a URL requested does not match an action route, the above

How to remove server header using middleware?

徘徊边缘 提交于 2019-12-04 16:43:13
问题 In ASP.NET Core 1.0 every response will include the header Server: Kestrel . I want to remove this header along with other header like X-Power-By using middleware. I know that we can remove Kestrel header in host configuration by setting the following but I want to do it using middleware (actually when we have Httpmodule we can do like this so I am learning same thing). I tried my bit it did not work. new WebHostBuilder() .UseKestrel(c => c.AddServerHeader = false) Tried code: public class

EF Core Add function returns negative id

限于喜欢 提交于 2019-12-04 16:30:58
问题 I noticed a weird thing today when I tried to save an entity and return its id in EF Core Before: After: I was thinking about if it was before calling saveChanges() but it works with another entity with a similar setup. ps: I use unit of work to save all changes at the end. What was the reason? 回答1: It will be negative until you save your changes. Just call Save on the context. _dbContext.Locations.Add(location); _dbContext.Save(); After the save, you will have the ID which is in the database

Run NUnit tests in dotnet core

偶尔善良 提交于 2019-12-04 15:14:15
问题 I am trying to run unit tests for my c# project with dotnet core. I am using docker container for runtime. Dockerfile FROM microsoft/dotnet:0.0.1-alpha RUN mkdir /src WORKDIR /src ADD . /src RUN dotnet restore "NUnit" and "NUnit.Runners" have been added into project.json "version": "1.0.0-*", "compilationOptions": { "emitEntryPoint": true }, "dependencies": { "NETStandard.Library": "1.0.0-rc2-23811", "NUnit": "3.2.0", "NUnit.Runners": "3.2.0" }, "frameworks": { "dnxcore50": { } } Run dotnet

Does ASP.NET Core web application targeting full dotnet framework work in IIS?

拜拜、爱过 提交于 2019-12-04 07:57:16
I am trying to publish an ASP.NET Core application developed on top of full framework (4.6.1) to IIS. Right now the code is just the base template code created using the Visual Studio "ASP.NET Core (.NET Framework) option. The code compiles fine but when I publish it to Local IIS, it fails to start. I get error like Unhandled Exception: System.TypeInitializationException: The type initializer for 'Microsoft.Extensions.PlatformAbstractions.PlatformServices' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Reflection.TypeExtensions, Version=4.0.0

Instantiating IOptions<> in xunit

[亡魂溺海] 提交于 2019-12-04 07:41:06
I'm trying to write an xunit test for a class (in a .net Core project) that looks something like: public Class FoodStore:IFoodStore { FoodList foodItems; public FoodStore(IOptions<FoodList> foodItems) { this.foodItems = foodItems; } public bool IsFoodItemPresentInList(string foodItemId) { //Logic to search from Food List } }` Note: FoodList is actually a json file, containing data, that is loaded and configured in the Startup class. How can I write an xunit test with appropriate dependency injection to test the IsFoodItemPresentInList method ? You could use OptionsWrapper<T> class to fake your

How to inject IHttpContextAccessor into Autofac TenantIdentificationStrategy

天涯浪子 提交于 2019-12-04 05:14:09
I am migrating my multitenant application from Webapi to aspnet core. In webapi version I was using TenantIdentificationStrategy that identified tenants based on request path on HttpContext . Moving to aspnet core, I am able to wire-up autofac successfully. I am not able to figure out how to wireup the tenant strategy. I tried injecting IHttpContextAccessor in ConfigureServices as services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); and my strategy looks like this public class AssetClassIdentificationStrategy: ITenantIdentificationStrategy { private readonly IHttpContextAccessor

'IServiceCollection' does not contain a definition for 'AddSession'

拈花ヽ惹草 提交于 2019-12-04 04:41:30
I am getting an error while adding 'AddSession' in ASP.Net Core 1.1 using VS2017. 'IServiceCollection' does not contain a definition for 'AddSession' and no extension method 'AddSession' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?) .csproj <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> </PropertyGroup> <PropertyGroup> <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback> </PropertyGroup> <ItemGroup>

Typeconverter does not work in asp.net core

坚强是说给别人听的谎言 提交于 2019-12-04 02:59:46
问题 I have Amount stored in the database as decimal . I want to show that value on UI with thousand separator. I can add [DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)] attribute on amount property and that would display number with thousand separator however when i POST the value back to server, the MVC model binding would not work because of commas. I have created a custom type converter that converts from decimal to string and then string to decimal public class