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.

Filter by
Sorted by
Tagged with
657 votes
18 answers
550k 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 ...
jltrem's user avatar
  • 12.3k
601 votes
9 answers
574k 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 ...
Dave New's user avatar
  • 39.4k
580 votes
42 answers
435k 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 { } ...
LP13's user avatar
  • 32.4k
513 votes
25 answers
1.0m 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 ...
kimbaudi's user avatar
  • 14.5k
411 votes
26 answers
384k 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.
eadam's user avatar
  • 24.7k
382 votes
20 answers
255k views

ASP.NET Core Get Json Array using IConfiguration

In appsettings.json { "MyArray": [ "str1", "str2", "str3" ] } In Startup.cs public void ConfigureServices(...
Max's user avatar
  • 4,719
360 votes
16 answers
254k 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 ...
Muhammad Rehan Saeed's user avatar
353 votes
17 answers
286k 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 : ...
Bart Verkoeijen's user avatar
346 votes
13 answers
413k 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(...
Mickael Caruso's user avatar
346 votes
24 answers
715k 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": { &...
aman's user avatar
  • 6,022
297 votes
24 answers
441k 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.
MRainzo's user avatar
  • 3,920
240 votes
18 answers
125k 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 ...
kevskree's user avatar
  • 4,512
234 votes
7 answers
349k 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 ...
Sam's user avatar
  • 28.6k
222 votes
11 answers
311k 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 ...
LP13's user avatar
  • 32.4k
218 votes
17 answers
215k 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 ...
drpdrp's user avatar
  • 2,462
199 votes
6 answers
308k 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 ...
RonC's user avatar
  • 32.6k
192 votes
30 answers
345k 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 ...
dr b's user avatar
  • 2,129
180 votes
25 answers
103k 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/...
jao's user avatar
  • 18.5k
180 votes
8 answers
159k 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?
eadam's user avatar
  • 24.7k
175 votes
4 answers
86k 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 ...
Sane's user avatar
  • 2,484
172 votes
4 answers
146k 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 ...
Elvin Mammadov's user avatar
168 votes
6 answers
202k 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 ...
Gabriel Castillo Prada's user avatar
166 votes
2 answers
68k 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;...
Christophe Gigax's user avatar
165 votes
7 answers
102k 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 ...
Muhammad Rehan Saeed's user avatar
157 votes
13 answers
214k 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?
oneNiceFriend's user avatar
157 votes
7 answers
64k 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: ...
mariocatch's user avatar
  • 8,515
151 votes
5 answers
159k 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/...
ProfK's user avatar
  • 50.2k
148 votes
20 answers
142k 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. ...
birdus's user avatar
  • 7,304
146 votes
6 answers
111k 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<...
Kemal Tezer Dilsiz's user avatar
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....
Felix's user avatar
  • 9,563
137 votes
7 answers
266k 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....
jbraun's user avatar
  • 1,548
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 ...
xr280xr's user avatar
  • 13k
132 votes
15 answers
192k 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 ...
Itsathere's user avatar
  • 1,420
131 votes
5 answers
168k 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 ...
Oleksandr Nahirniak's user avatar
126 votes
6 answers
122k 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 ...
JTW's user avatar
  • 3,645
125 votes
7 answers
169k 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 ...
jao's user avatar
  • 18.5k
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 ....
sibvic's user avatar
  • 1,642
118 votes
7 answers
86k 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 ...
Robert Paulsen's user avatar
116 votes
7 answers
141k 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 ...
Keith's user avatar
  • 153k
115 votes
30 answers
132k 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 ...
ruhm's user avatar
  • 1,367
113 votes
8 answers
87k 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
Ivandro Jao's user avatar
  • 2,841
112 votes
9 answers
103k 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(...
David Sharpe's user avatar
  • 1,149
112 votes
20 answers
255k 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)....
eadam's user avatar
  • 24.7k
108 votes
3 answers
171k 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 ...
severin's user avatar
  • 5,363
108 votes
4 answers
56k 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 ...
Nikolay Kostov's user avatar
108 votes
10 answers
98k 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 ...
Dhanuka777's user avatar
  • 8,516
104 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
Muhammad Rehan Saeed's user avatar
96 votes
4 answers
151k 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 ...
rgvassar's user avatar
  • 5,743
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....
Sauron's user avatar
  • 2,256
95 votes
2 answers
64k 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(...
dfmetro's user avatar
  • 4,542

1
2 3 4 5
296