asp.net-core-2.0

How to read/get a PropertyGroup value from a .csproj file using C# in a .NET Core 2 classlib project?

[亡魂溺海] 提交于 2019-12-06 07:13:26
问题 I want to get the value of the element <Location>SourceFiles/ConnectionStrings.json</Location> that is child of <PropertyGroup /> using C#. This is located at the .csproj file for a .NET Core 2 classlib project. The structure is as follow: <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <Location>SharedSettingsProvider.SourceFiles/ConnectionStrings.json</Location> </PropertyGroup> Which class can I use from .NET Core libraries to achieve this? (not .NET framework) Update 1: I

.Net Core with Angular 4 : 404 error when I refresh the browser

允我心安 提交于 2019-12-06 05:26:31
I have built an angular 4 application with .Net core 2.0, visual studio 2017, and deployed to production server on IIS web server. The routing is working fine on localhost when refresh,but not working on live. I saw some answers in stackoverflow for IIS in web.config file. But I don't have web.config file under my project structure. can I add it manually or how can I fix it. <system.webServer> <rewrite> <rules> <rule name="Main Rule" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="

Configure Column Options for Serilog Sinks MsSqlServer in AppSettings.json

浪尽此生 提交于 2019-12-06 04:50:08
I'm trying to determine if it's possible to configure the column options for serilog sink mssqlserver in the appsettings.json file for an ASP.Net Core 2 project. I create and configure the logger in the Program.cs file. public static IConfiguration Configuration { get; } = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true) .AddEnvironmentVariables() .Build(); public static

How to throw ForbiddenException in ASP.NET Core 2 Instead of using AccessDeniedPath

不羁岁月 提交于 2019-12-06 04:32:05
I am working on an ASP.NET Core 2 web application. I am handling Access Denied page for [Authorize (roles OR policies)] pages. By default, Instead of showing the original URL and returning 403 status, ASP.NET Core 2.0 redirects the request to an AccessDenied page with status is 302 -> This is not what I want. Instead of redirecting AccessDenied page. I want ASP.NET Core throws my custom ForbiddenException exception so I can handle unauthorized accesses like I do for Unhandled exceptions. Here is my authentication configuration: services.AddAuthentication(options => { options

.NET Core Hosting Bundle

半腔热情 提交于 2019-12-06 02:23:43
问题 As far as I understood the Docs, the .NET Core Hosting Bundle installs the . NET Core Runtime , .NET Core Library and the ASP.NET Core Module . It seems that there's a Bundle for every version of the .NET Core Runtime ( 2.0.6 , 2.0.7 , ...). If I have a self contained deployment of my app, I still need the ASP.NET Core Module. However I don't see that the Module can be downloaded separately without the full bundle. Is there a place where I can download it ? If not: What's the point of having

DbContext Dependency Injection outside of MVC project

只谈情不闲聊 提交于 2019-12-06 00:36:46
I have a C# solution with two projects, ProductStore.Web and ProductStore.Data, both targeting .NET Core 2.0. I have my HomeController and CustomerRepository as follows (I've set it up in the HomeController for speed, customer creation will be in the customer controller, but not yet scaffold-ed it out): namespace ProductStore.Web.Controllers { public class HomeController : Controller { private readonly DatabaseContext _context; public HomeController(DatabaseContext context) { _context = context; } public IActionResult Index() { ICustomerRepository<Customer> cr = new CustomerRepository(_context

When using an API route, return Http Response 401 instead of redirect to login page when not authorised

眉间皱痕 提交于 2019-12-06 00:28:51
I'm building an ASP.NET Core 2.0 website using MVC and WebAPI to provide access to a series of microservices. Where a WebAPI controller requires a user to be authenticated and authorised (using the Authorize attribute), any unauthorised or not-logged in user gets the response back as the entire HTML for the MVC login page. When unauthorised users access the API, I would like to return the HTTP status code 401 and its associated error message in the response, instead of an entire HTML page. I've looked at a few existing questions and noticed that they either refer to ASP.NET MVC (such as

How to precompile views in ASP.NET Core 2.0?

天大地大妈咪最大 提交于 2019-12-05 21:54:02
问题 I set up my solution according to this article. I left out some of the things because according to this, ASP.NET Core 2.0 precompiles views by default. In the end, I publish it to a folder, which ends successfully, but my precompiledviews.dll is missing. I tried setting it explicitly in .csproj, but no luck. Edit: Both of the projects inside the solution are just default MVC templates. 回答1: I bet you use Self-contained deployment, i.e. publish with command like dotnet publish --configuration

Global Variables in ASP.Net Core 2

天大地大妈咪最大 提交于 2019-12-05 21:39:54
问题 I am developing a web application in ASP.NET Core and currently have a large set of keys, such as stripe account keys. Instead of having them spread throughout the project in different classes I would like to place them all together in json where they could be accessed globally. I have tried placing them in appsettings.json but cannot access them anywhere. 回答1: I often do this kind of thing with connection strings and other global constants. First create a class for those variables that you

.Net Core 2.0 and xUnit doesn't run

蹲街弑〆低调 提交于 2019-12-05 19:17:43
VS2017 + Core 2.0. Created a brand new XUnit test project from template .csproj file: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" /> <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> <PackageReference Include="xunit" Version="2.3.1" /> <PackageReference Include="xunit.runner.console" Version="2.3.1" /> </ItemGroup> </Project> Test: public class Class1 { [Fact] public void Test1() { Assert.Equal(1,