All Questions
Tagged with .net-core asp.net-core-mvc
1,404
questions
240
votes
18
answers
124k
views
.NET Core MVC Page Not Refreshing After Changes
I'm building a .NET Core MVC on the latest version 2.2. I have a problem when I make changes to the CSHTML file and refresh the page, my changes are not reflected in the browser. I have to restart the ...
134
votes
3
answers
64k
views
I'm lost. What happened to ASP.NET MVC 5?
I've been keeping my head down working on various projects and apparently Microsoft has been busy making some big changes and it's confusing the hell out of me. ASP.NET Core first came onto my radar ...
118
votes
6
answers
85k
views
Dependency Injection with classes other than a Controller class
At this point I'm injecting things into my Controllers with ease, in some cases building my own ResolverServices class. Life is good.
What I cannot figure out how to do is get the framework to ...
78
votes
1
answer
28k
views
Replacement of AssemblyBuilder.DefineDynamicAssembly in .NET Core
How do I port the following code to .NET Core?
AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName(
Guid.NewGuid().ToString()),
...
77
votes
6
answers
56k
views
Do we need interfaces for dependency injection?
I have an ASP.NET Core application. The application has few helper classes that does some work. Each class has different signature method. I see lot of .net core examples online that create interface ...
77
votes
6
answers
93k
views
Encrypted configuration in ASP.NET Core
With web.config going away, what is the preferred way to store sensitive info (passwords, tokens) in the configurations of a web app built using ASP.NET Core?
Is there a way to automatically get ...
73
votes
7
answers
61k
views
How to get IOptions in ConfigureServices method?
I have asp.net core application. I want to use IOptions pattern to inject values from appsettings.json. So I have a class SecurityHeaderOptions, and also have target class SecurityHeadersBuilder whose ...
66
votes
7
answers
113k
views
Access from class library to appsetting.json in Asp.net-core
I am trying to access appsetting.json file from a class library. So far the solution that I found is to create a configuration class implementing interface IConfiguration from Microsoft.Extensions....
65
votes
5
answers
51k
views
How to set .NET Core in #if statement for compilation
I created a multi-targeted framework project. I use something like this:
#if NET40
Console.WriteLine("Hello from .NET Core 4");
#endif
But I can't find a wildcard for .NET Core. I tried:
...
63
votes
13
answers
25k
views
No executables found matching command 'dotnet-aspnet-codegenerator'"
When trying to add a Controller in an ASP.NET Core project using Visual Studio 15 Enterprise with Update 3, I get the error below:
"The was an error running the selected code generator: No ...
60
votes
5
answers
128k
views
How to get absolute path in ASP.Net Core alternative way for Server.MapPath
How to get absolute path in ASP net core alternative way for Server.MapPath
I have tried to use IHostingEnvironment but it doesn't give proper result.
IHostingEnvironment env = new ...
52
votes
2
answers
34k
views
Doesn't ASP.NET Core have an InternalServerError()?
The Microsoft.AspNetCore.Mvc.Controller base class doesn't have an equivalent of BadRequest(), Ok(), NoContent(), etc. for returning HTTP 500 status codes.
Why can't we do?
try{
oops();
}
catch(...
50
votes
1
answer
27k
views
What is difference between ObjectResult and JsonResult
There are two classes in Microsoft.AspNetCore.Mvc namespace:
ObjectResult and JsonResult.
Both convert the returned object in the JSON format.
What is difference between them and what is the ...
44
votes
5
answers
76k
views
How to get the Values from a Task<IActionResult> returned through an API for Unit Testing
I have created an API using ASP.NET MVC Core v2.1. One of my HttpGet methods is set up as follows:
public async Task<IActionResult> GetConfiguration([FromRoute] int? id)
{
try
{
...
44
votes
5
answers
22k
views
Change the JSON serialization settings of a single ASP.NET Core controller
I'm having two controller controllers: ControllerA and ControllerB. The base class of each controller is Controller.
The ControllerA needs to return JSON in the default format (camelCase). The ...
39
votes
3
answers
59k
views
How to register dependency injection with generic types? (.net core)
I have an asp.net core web app with multiple parameters in appSettings.json file.
I didnt' want to have services having IOptions<MyObject> in the constructor.
I wanted MyObject in the ...
39
votes
4
answers
58k
views
Get the full route to current action
I have a simple API with basic routing. It was setup using the default Visual Studio 2015 ASP.NET Core API template.
I have this controller and action:
[Route("api/[controller]")]
public class ...
38
votes
4
answers
60k
views
Inject generic interface in .NET Core
I want to inject this interface to my controllers:
public interface IDatabaseService<T>
{
IEnumerable<T> GetList();
...
}
I want to use generic, because in my WebApi project i ...
33
votes
6
answers
16k
views
Has something replaced bundleconfig.json in ASP.NET Core MVC 2.1?
When I create a new ASP.NET MVC Core targeting 2.1 RC1, it doesn't create the bundleconfig.json file which is used for bundling and minification. This file is created if I target 2.0.
Here is a ...
32
votes
7
answers
32k
views
Logging within Program.cs
Is it possible to get an ILogger inside Program.cs Main method? I want to pass it on to a service that's created inside that method.
I've only found this on SO How do I write logs from within Startup....
31
votes
10
answers
18k
views
Trying to add AutoMapper to .NetCore1.1 - not recognising services.AddAutoMapper()
I've installed the following Nuget packages into my project:
Automapper
AutoMapper.Extensions.Microsoft.DependencyInjection
I have added the line to ConfigureServices in Startup.cs.
public void ...
31
votes
5
answers
83k
views
Required query string parameter in ASP.NET Core
Using ASP.NET Core 1.1 with VS2015 (sdk 1.0.0-preview2-003131), I have the following controller:
public class QueryParameters
{
public int A { get; set; }
public int B { get; set; }
}
[Route(...
31
votes
8
answers
31k
views
How to connect ASP.Net Core to a SQL Server Docker container on Mac
I am developing an ASP.Net core MVC application using Visual Studio 2017 on Mac. However, I am facing some problems to connect to an instance of SQL Server that is running on a Docker container.
...
31
votes
2
answers
17k
views
AutoValidateAntiForgeryToken vs. ValidateAntiForgeryToken
I was trying to secure a post method against side scripting just now through providing an anti forgery token but noticed, in .Net Core there is another attribute named as AutoAntiForgeryToken. The XML ...
29
votes
2
answers
20k
views
How should I manage DbContext Lifetime in MVC Core?
From the Documentation
Entity Framework contexts should be added to the services container
using the Scoped lifetime. This is taken care of automatically if you
use the helper methods as shown ...
29
votes
4
answers
19k
views
Authenticate against Active Directory in .NET Core 1.0 application?
With the recent release of .NET Core 1.0, we're in the process of migrating our RC1 applications to the final release. The only piece we can't seem to figure out is how to integrate Active Directory ...
27
votes
4
answers
32k
views
Can I access a database during startup in ASP.NET Core?
I have recently been working on a .NET Core web API. I have just attempted authentication using JWT, by following the guide on https://stormpath.com/blog/token-authentication-asp-net-core.
All was ...
27
votes
4
answers
27k
views
Add a header to all responses in ASP.NET Core MVC
I would like to know how I can add Access-Control-Allow-Origin:* to my headers.
I've tried this unsuccessfully:
app.Use((context, next) =>
{
context.Response.Headers.Add("Access-Control-Allow-...
27
votes
5
answers
10k
views
.NET Core Identity as UI canceling Register
I want to cancel the 'Register' option in a .NET Core 2.1 + Identity as UI application.
I can of course simply remove the button from the page, question is - is that safe ?
If not what are my other ...
27
votes
3
answers
22k
views
Unit testing an AuthorizeAttribute on an ASP.NET Core MVC API controller
I have a ASP.NET Core MVC API with controllers that need to be unit tested.
Controller:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
...
25
votes
2
answers
5k
views
How to configure multiple ASPNETCORE_ENVIRONMENT on same machine?
I have ASP.NET core web application. I have configured the web application on our web server and set the ASPNETCORE_ENVIRONMENT variable to Development. I set this variable at machine level like shown ...
24
votes
2
answers
14k
views
NETCORE MVC - How to work with nested, multi-parameterized routes
Looking for best practices when working with nested routes in .NET Core MVC.
Let's say CampusController.cs works with a base model:
[Route("api/campus/")]
public class CampusController : Controller
{...
24
votes
6
answers
17k
views
.NET Core Docker Image for SPA Applications
What is the correct Docker image to use when creating a new ASP.NET Core MVC app, specifically with the React/Redux (or other Node.js required) template? If not a specific image, what commands or ...
24
votes
1
answer
16k
views
Opening Visual Studio 2017 projects in Visual Studio 2015
I've moved on to Visual Studio 2017 RC, but we still have people working on Visual Studio 2015. Currently when I create a project on 2017, it won't work on 2015. When the project is opened on VS2015 I ...
22
votes
4
answers
29k
views
How to install Font Awesome in ASP.NET Core 2.2 using Visual Studio 2019 [closed]
I am struggling to find any up to date installation guide for installing Font Awesome in ASP.NET Core 2.2
I've tried a manual file import to the project folder directory, then tried the NuGet package ...
22
votes
1
answer
11k
views
.Net Core 3 IStringLocalizer.WithCulture(CultureInfo) is obsolete
I've upgraded a project from .Net Core 2.2 to .Net Core 3.0.
After trying to fix all the warnings and errors I'm now trying to find a solution to this warning:
'IStringLocalizer.WithCulture(...
20
votes
2
answers
24k
views
How do I put data in a MemoryCache on startup?
At startup, I want to create a store of static data for my web app. So I eventually stumbled onto Microsoft.Extensions.Caching.Memory.MemoryCache. After building the feature that uses the MemoryCache, ...
20
votes
8
answers
17k
views
Microsoft.AspNetCore.Antiforgery was not found
I'm deploying a asp.net core 2.0 website to IIS 10.
I've made sure that my app is using the correct configuration for ISS in the program.settings file.
public class Program
{
public static void ...
19
votes
2
answers
54k
views
ASP.NET Core 2.2: Unable to resolve service for type 'AutoMapper.IMapper'
ASP.NET Core (Version: 2.2.102)
I am building an API to return Portos and Especies, but anytime that I access /api/portos (as defined in the controller), I get this error:
...
19
votes
6
answers
9k
views
.net core : incomplete JSON response
I'm trying to build simple API for training, in my database I got users (firstname, lastname, email password, list<sports>) and sports ( name, userID).
All is okay when I want to get my users, I ...
19
votes
1
answer
21k
views
How to create thumbnail image in .net core? Using the help of IFormFile
I need to create a thumbnail image from the original image and need to save both images in the local folder.
I am using html file control for uploading the image
<input type="file" class="form-...
18
votes
4
answers
22k
views
An instance of analyzer Microsoft.AspNetCore.Mvc.Analyzers.AvoidHtmlPartialAnalyzer cannot be created from C:\
I'm updating a VS2017 to use ASP.NET Core 2.1 (installing dot.net core sdk 2.1 and Microsoft.Net.Compilers 2.8.2) and I am receiving some warnings about analyzers not being able to be created after ...
18
votes
1
answer
13k
views
How can I return a response in ASP.NET Core MVC middleware using MVC's content negotiation?
I have some ASP.NET Core MVC middleware to catch unhandled exceptions that I would like to return a response from.
While it is easy to just httpContext.Response.WriteAsync to write a string and e.g. ...
17
votes
1
answer
12k
views
how to implement google login in .net core without an entityframework provider
I am implementing Google login for my .net core site.
In this code
var properties = signInManager.ConfigureExternalAuthenticationProperties("Google", redirectUrl);
return new ChallengeResult("...
16
votes
5
answers
4k
views
Asp.Net Core API disable startup complete message
As part of my application I have a .Net Core API project. Unlike most cases where this project would run as its own process, I have the API run in a thread, among others, in a single process. Also for ...
16
votes
4
answers
25k
views
AddEntityFrameworkStores can only be called with a role that derives from IdentityRole in .NET Core 2.0
I have changed a project from the .NET Core 1.1 to 2.0 version, but I'm getting an error from the Identity, when It tries to add the stores:
services.AddIdentity<ApplicationUser, IdentityRole<...
15
votes
5
answers
22k
views
Injecting Single Instance HttpClient with specific HttpMessageHandler
As part of an ASP.Net Core project that I am working on I have a requirement to communicate with a number of different Rest based API Endpoints from within my WebApi. To achieve this I am using a ...
15
votes
2
answers
36k
views
.NET Core Web API: multiple [FromBody]?
I am converting code that was written in ASP.NET MVC to ASP.NET Core MVC. While I was converting the code, I encountered a problem. We used a method that has multiple parameters like this:
[HttpPost] ...
15
votes
3
answers
21k
views
How to update appsettings.json based on publish profile using .NET Core?
I'm currently making the switch from .NET Framework to .NET Core. In the past, all of my application settings were in the Web.config file. When I added a new publish profile, I could right click and ...
15
votes
4
answers
25k
views
How to read a connectionString WITH PROVIDER in .NET Core?
I added
.AddJsonFile("Connections.json", optional: true, reloadOnChange: true)
in
public Startup(IHostingEnvironment env)
Connections.json contains:
{
"ConnectionStrings": {
"...