asp.net-core-mvc

ASP.NET Core 3.0 [FromBody] string content returns “The JSON value could not be converted to System.String.”

北战南征 提交于 2019-12-08 04:37:23
问题 Using [FromBody] string content on an ApiController in ASP.NET Core 3.0 returns a validation error: {"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title":"One or more validation errors occurred.", "status":400, "traceId":"|9dd96d96-4e64bafba4ba0245.", "errors":{"$":["The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."]}} when the client post data with content-type : application/json How do I get the raw json data as a

How to inject services in a controller inside a reusable Razor Class Library

六眼飞鱼酱① 提交于 2019-12-08 04:25:56
问题 I am using a Razor Class Library for making a reusable complex View (which includes its controller and several View Components) that can be used across several ASP.NET Core MVC projects. The problem is that the controller use dependency injection (a custom service called "GatewayProxy" and string localization). What is the correct way to inject services into a controller inside a RCL? Here is the structure of my RCL: Here is the exception: 回答1: You mentioned how you fixed this by adding the

Some value are null AJAX call to controller ASP.Net Core

人走茶凉 提交于 2019-12-08 03:38:16
问题 I'm trying to pass an object through a form to my controller with an AJAX call. Here is the object, everything returns null / 0 except AuctionId : public class BidModel { [JsonProperty("BudID")] public string BidId { get; set; } [JsonProperty("Summa")] public int Amount { get; set; } [JsonProperty("AuktionID")] public string AuctionId { get; set; } [JsonProperty("Budgivare")] public string Bidder { get; set; } } The Form: <form id="createBid"> <div id="frmBid" class="form-inline"> <input name

MVC6 Web Api - Return Plain Text

▼魔方 西西 提交于 2019-12-08 02:55:51
问题 I have looked at other similar questions (such as this one Is there a way to force ASP.NET Web API to return plain text?) on SO but they all seem to address WebAPI 1 or 2, not the latest version you use with MVC6. I need to return plain text on one of my Web API controllers. Only one - the others should keep returning JSON. This controller is used for dev purposes to output a list of records in a database, which will be imported into a traffic load generator. This tool takes CSV as input so I

Build a .NET core application while running Kestrel server

▼魔方 西西 提交于 2019-12-08 02:51:49
问题 In classic .NET applications, I'd set up my local IIS to run the specified application. I could build the application and hit the endpoint to see the change immediately. I did not need to launch the debugger every time. I want to achieve this with .NET core using Kestrel server. I'm able to run my application by running dotnet run from the command line. However, when I try to build, the files are in use and the build fails. Are there any development configuration options that do not lock down

Change the connection string dynamically (per request) on Entity Framework 7 / MVC 6

半世苍凉 提交于 2019-12-08 02:14:41
问题 I have a MVC 6 application on which I need to connect to a different database (i.e. physical file but same schema) depending on who is accessing to it. That is: each customer of the web application will have it's data isolated in an SQL database (on Azure, with different performances, price levels, etc.) but all those databases will share the same relational schema and of course, the Entity Framework context class. var cadConexion = @"Server=(localdb)\mssqllocaldb;Database=DBforCustomer1

Issue with Default camelCase serialization of All Caps property names to JSON in ASP.Net core

点点圈 提交于 2019-12-08 01:42:43
问题 I have an issue with the default serialization CamelCasing behavior of .Net Core and was hoping to see if someone else faced the same issue and what work around they used. Property Names like FOO12 or FOO1 are incorrectly serialized to something like foO12 or foO1 When infact they should probably be done as foo12 or foo1. I have used a workaround of adding the following Attribute but was hoping somebody might have a better answer to this issue: [JsonProperty(PropertyName = "foo12")] 回答1: Json

Fallback route for MVC Areas

假如想象 提交于 2019-12-07 21:47:07
问题 Using asp.net core MVC application here. I have my areas defined and routing works as expected. One thing i would like to do, is have a fallback route in case the area does not exist. I have the following structure: APP -- Areas -- SharedArea -- Controllers -- LoginController -- UserController -- AnotherController -- AndSoOnController -- SomeArea1 -- Controllers -- HomeController -- SomeArea2 -- Controllers -- HomeController -- LoginController My ultimate goal, is to have shared controllers

ASP NET Core MVC - How to configure Out of Process Session State?

橙三吉。 提交于 2019-12-07 21:01:52
问题 Is there a way to configure out of process session state (using the the Windows State server or SQL Server) with ASP.NET Core MVC? 回答1: What is important is that session data is backed by a cache. You need to add IDistributedCache implementation to your application services, instead of the in-memory cache, i.e: for SQL Server: services.AddDistributedSqlServerCache(options => { options.ConnectionString = @"your_connection_string"; options.SchemaName = "dbo"; options.TableName = "TestCache"; })

ASP.NET MVC 6 application's virtual application root path

我怕爱的太早我们不能终老 提交于 2019-12-07 19:28:15
问题 How do I get the virtual root path of the application on the server? In another words: how can I do the following in ASP.NET MVC 6? HttpContext.Current.Request.ApplicationPath 回答1: What you need can be achieved with @Url.Content("~/") , which will map "~" to your virtual application root path. Having a look at the source code, it seems to do so using the HttpContext.Request.PathBase property: public virtual string Content(string contentPath) { if (string.IsNullOrEmpty(contentPath)) { return