Questions tagged [asp.net-core-mvc]
ASP.NET Core MVC is a lightweight presentation framework for creating dynamic websites with ASP.NET Core. It allows for creating controller based MVC sites, or view model based Razor pages. MVC includes features like routing, model binding and validation, filters, web APIs and the Razor view engine.
asp.net-core-mvc
14,731
questions
657
votes
18
answers
547k
views
How do you create a custom AuthorizeAttribute in ASP.NET Core?
I'm trying to make a custom authorization attribute in ASP.NET Core. In previous versions it was possible to override bool AuthorizeCore(HttpContextBase httpContext). But this no longer exists in ...
599
votes
9
answers
569k
views
Resolving instances with ASP.NET Core DI from within ConfigureServices
How do I manually resolve a type using the ASP.NET Core MVC built-in dependency injection framework?
Setting up the container is easy enough:
public void ConfigureServices(IServiceCollection ...
576
votes
42
answers
432k
views
How to register multiple implementations of the same interface in Asp.Net Core?
I have services that are derived from the same interface.
public interface IService { }
public class ServiceA : IService { }
public class ServiceB : IService { }
public class ServiceC : IService { }
...
511
votes
25
answers
996k
views
Dependency Injection error: Unable to resolve service for type while attempting to activate, while class is registered
I created an .NET Core MVC application and use Dependency Injection and Repository Pattern to inject a repository to my controller. However, I am getting an error:
InvalidOperationException: Unable ...
409
votes
26
answers
381k
views
How do I get client IP address in ASP.NET Core?
Can you please let me know how to get client IP address in ASP.NET when using MVC 6.
Request.ServerVariables["REMOTE_ADDR"] does not work.
378
votes
20
answers
253k
views
ASP.NET Core Get Json Array using IConfiguration
In appsettings.json
{
"MyArray": [
"str1",
"str2",
"str3"
]
}
In Startup.cs
public void ConfigureServices(...
359
votes
16
answers
252k
views
How to get the Development/Staging/production Hosting Environment in ConfigureServices
How do I get the Development/Staging/production Hosting Environment in the ConfigureServices method in Startup?
public void ConfigureServices(IServiceCollection services)
{
// Which environment ...
351
votes
16
answers
284k
views
ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response
Sending a form POST HTTP request (Content-Type: application/x-www-form-urlencoded) to the below controller results into a HTTP 415 Unsupported Media Type response.
public class MyController : ...
345
votes
13
answers
410k
views
How to return HTTP 500 from ASP.NET Core RC2 Web Api?
Back in RC1, I would do this:
[HttpPost]
public IActionResult Post([FromBody]string something)
{
try{
// ...
}
catch(Exception e)
{
return new HttpStatusCodeResult(...
343
votes
24
answers
709k
views
Getting value from appsettings.json in .NET Core
I am not sure what am I missing here, but I am not able to get the values from my appsettings.json in my .NET Core application. I have my appsettings.json as:
{
"AppSettings": {
&...
297
votes
24
answers
438k
views
How to get the current logged in user ID in ASP.NET Core?
I've done this before with MVC5 using User.Identity.GetUserId() but that doesn't seem to work here.
The User.Identity doesn't have the GetUserId() method.
I am using Microsoft.AspNet.Identity.
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 ...
233
votes
7
answers
348k
views
Select Tag Helper in ASP.NET Core MVC
I need some help with the select tag helper in ASP.NET Core.
I have a list of employees that I'm trying to bind to a select tag helper. My employees are in a List<Employee> EmployeesList and ...
221
votes
11
answers
307k
views
How do I access Configuration in any class in ASP.NET Core?
I have gone through configuration documentation on ASP.NET core. Documentation says you can access configuration from anywhere in the application.
Below is Startup.cs created by template
public ...
218
votes
17
answers
212k
views
Publish to IIS, setting Environment Variable
Reading these two questions/answers I was able to run an Asp.net 5 app on IIS 8.5 server.
Asp.net vNext early beta publish to IIS in windows server
How to configure an MVC6 app to work on IIS?
The ...
198
votes
6
answers
306k
views
How to return a specific status code and no contents from Controller?
I want the example controller below to return a status code 418 with no contents. Setting the status code is easy enough but then it seems like there is something that needs to be done to signal the ...
188
votes
30
answers
340k
views
500 Error when setting up Swagger in asp .net CORE / MVC 6 app
I'm trying to setup a basic swagger API doc in a new asp .net CORE / MVC 6 project and receiving a 500 error from the swagger UI:
500 : http://localhost:4405/swagger/v1/swagger.json
My startup class ...
180
votes
25
answers
102k
views
Duplicate 'Content' items were included. The .NET SDK includes 'Content' items from your project directory by default
Whenever I add a javascript or css file to my asp.net core project and I execute dotnet run in my bash terminal, I get the following error:
/usr/share/dotnet/sdk/1.0.1/Sdks/Microsoft.NET.Sdk/build/...
179
votes
8
answers
158k
views
How to get user Browser name ( user-agent ) in Asp.net Core?
Can you please let me know how to get the browser's name that the client is using in MVC 6, ASP.NET 5?
175
votes
4
answers
85k
views
Use multiple JWT Bearer Authentication
Is it possible to support multiple JWT Token issuers in ASP.NET Core 2?
I want to provide an API for external service and I need to use two sources of JWT tokens - Firebase and custom JWT token ...
169
votes
4
answers
145k
views
How to add link parameter to asp tag helpers in ASP.NET Core MVC
I have a lot of experience with ASP.NET MVC 1-5. Now I learn ASP.NET Core MVC and have to pass a parameter to link in page. For example I have the following Action
[HttpGet]
public ActionResult ...
168
votes
6
answers
200k
views
Can I generate script of a migration with EF code first and .net core
I'm building a MVC application with .Net Core and I need to generate the script of a migration.
With EF6 I did run the command
update-database -script
but when I try to do the same with .net Core is ...
166
votes
2
answers
67k
views
Include several references on the second level
Assume we have this model :
public class Tiers
{
public List<Contact> Contacts { get; set; }
}
and
public class Contact
{
public int Id { get; set; }
public Tiers Tiers { get; set;...
164
votes
7
answers
101k
views
How to resolve IOptions instance inside ConfigureServices?
Is it possible to resolve an instance of IOptions<AppSettings> from the ConfigureServices method in Startup? The documentation explicitly says:
Don't use IOptions<TOptions> or ...
157
votes
13
answers
213k
views
How to get current url in view in asp.net core 1.0
In previous versions of asp.net, we could use
@Request.Url.AbsoluteUri
But it seems it's changed. How can we do that in asp.net core 1.0?
154
votes
7
answers
63k
views
How do you enforce lowercase routing in ASP.NET Core?
In ASP.NET 4 this was as easy as routes.LowercaseUrls = true; in the RegisterRoutes handler for the app.
I cannot find an equivalent in ASP.NET Core for achieving this. I'd think it would be here:
...
150
votes
5
answers
157k
views
What is the equivalent of Server.MapPath in ASP.NET Core?
I have this line in some code I want to copy into my controller, but the compiler complains that
The name 'Server' does not exist in the current context
var UploadPath = Server.MapPath("~/App_Data/...
147
votes
20
answers
141k
views
ASP.NET Core—access Configuration from static class
I want a simple static class that accesses the Configuration object. All the config info is already read in from the appsettings.json file in the Startup class. I just need an easy way to access it. ...
146
votes
5
answers
110k
views
How can I get Url Referrer in ASP.NET Core MVC?
I am trying to migrate an ASP.NET MVC webform to ASP.NET Core MVC. Currently, I am having trouble with the Request.UrlReferrer class.
The original line is:
[HttpPost]
public async Task<...
138
votes
8
answers
43k
views
Mocking IPrincipal in ASP.NET Core
I have an ASP.NET MVC Core application that I am writing unit tests for. One of the action methods uses User name for some functionality:
SettingsViewModel svm = _context.MySettings(User.Identity....
137
votes
7
answers
264k
views
ASP.NET Core Identity - get current user
To get the currently logged in user in MVC5, all we had to do was:
using Microsoft.AspNet.Identity;
[Authorize]
public IHttpActionResult DoSomething() {
string currentUserId = User.Identity....
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 ...
131
votes
5
answers
166k
views
Dependency injection, inject with parameters
I'm using vNext implementation of DI.
How to pass parameters to constructor?
For example, i have class:
public class RedisCacheProvider : ICacheProvider
{
private readonly string ...
129
votes
15
answers
190k
views
Increase upload file size in Asp.Net core
Currently, I am working with Asp.Net Core and MVC6 need to upload file size unlimited. I have searched its solution but still not getting the actual answer.
I have tried this link
If anyone have any ...
126
votes
6
answers
121k
views
How to read ASP.NET Core Response.Body?
I've been struggling to get the Response.Body property from an ASP.NET Core action and the only solution I've been able to identify seems sub-optimal. The solution requires swapping out Response.Body ...
124
votes
7
answers
168k
views
How can I get the baseurl of my site in ASP.NET Core?
Say my website is hosted in the mywebsite folder of www.example.com and I visit https://www.example.com/mywebsite/home/about.
How do I get the base url part in an MVC controller? The part that I am ...
123
votes
6
answers
72k
views
WebUtility.HtmlDecode replacement in .NET Core
I need to decode HTML characters in .NET Core (MVC6). It looks like .NET Core doesn't have WebUtility.HtmlDecode function which everybody used for that purpose before. Is there a replacement exist in ....
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 ...
116
votes
7
answers
140k
views
Returning a 404 from an explicitly typed ASP.NET Core API controller (not IActionResult)
ASP.NET Core API controllers typically return explicit types (and do so by default if you create a new project), something like:
[Route("api/[controller]")]
public class ThingsController : Controller
...
115
votes
30
answers
131k
views
Unable to create migrations after upgrading to ASP.NET Core 2.0
After upgrading to ASP.NET Core 2.0, I can't seem to create migrations anymore.
I'm getting
"An error occurred while calling method 'BuildWebHost' on class
'Program'. Continuing without the ...
113
votes
8
answers
86k
views
What are the differences between app.UseRouting() and app.UseEndPoints()?
As I'm trying to understand them, It seem like they are both used to route/map the request to a certain endpoint
112
votes
20
answers
253k
views
How to run stored procedures in Entity Framework Core?
I am using EF Core 1.0 in an ASP.NET Core App. Can you please point me to the proper way of executing stored procedures? The old method with ObjectParameters and ((IObjectContextAdapter)this)....
111
votes
9
answers
102k
views
Using enum for dropdown list in ASP.NET MVC Core
I'm trying to create a dropdown list with an enum property in ASP.NET MVC Core using the tag helper in a Razor view:
Here is the model:
public class PersonalMember : Member
{
[Required, Display(...
108
votes
3
answers
169k
views
Read environment variables in ASP.NET Core
Running my ASP.NET Core application using DNX, I was able to set environment variables from the command line and then run it like this:
set ASPNET_ENV = Production
dnx web
Using the same approach in ...
108
votes
4
answers
55k
views
What is the purpose of the ConcurrencyStamp column in the AspNetUsers table in the new ASP.NET MVC 6 identity?
What is the purpose of the ConcurrencyStamp column in the AspNetUsers table in the new ASP.NET MVC 6 identity?
This is the database schema of the AspNetUsers table:
It is also there in the ...
108
votes
10
answers
97k
views
View POST request body in Application Insights
Is it possible to view POST request body in Application Insights?
I can see request details, but not the payload being posted in application insights. Do I have to track this with some coding?
I am ...
103
votes
4
answers
45k
views
System.Net.Http vs Microsoft.Net.Http
I am using ASP.NET Core. I want to use HttpClient but I noticed that there are two NuGet packages being offered. Which one do I use?
System.Net.Http
Microsoft.Net.Http
95
votes
3
answers
55k
views
Using MimeMapping in ASP.NET Core
I'm trying to move my old mvc5 project to asp net core.
Old code was:
public string ContentType
{
get
{
if (!string.IsNullOrEmpty(FileName))
return MimeMapping....
95
votes
2
answers
63k
views
Generic repository in ASP.NET Core without having a separate AddScoped line per table in Startup.cs?
I have a generic repository in my project.
Consider the following controller snippet
public class Lookup1Controller : Controller
{
readonly MyDbContext _db;
public Lookup1Controller(...
95
votes
4
answers
150k
views
Custom Authentication in ASP.Net-Core
I am working on a web app that needs to integrate with an existing user database. I would still like to use the [Authorize] attributes, but I don't want to use the Identity framework. If I did want to ...