asp.net-core-mvc

ASP.NET Core MVC - empty string to null when sending JSON to server

∥☆過路亽.° 提交于 2019-12-11 08:13:18
问题 When posting input data as a FormData to the ASP.NET Core MVC controller, by default empty string values are coerced to null values. However, when sending input data as JSON to the controller, empty string values remain as they are. This leads to different behavior when validating string properties. For example, description field is not bound to null , but to empty string on the server: { value: 1, description: "" } This in turn makes following model invalid, even though Description is not

Authentication in .net Core with ReactJS SPA

 ̄綄美尐妖づ 提交于 2019-12-11 08:07:41
问题 I trying add authentication to .net Core 2.1 application. Starting from scratch: We can create new web application with react using VS template. In this tempate we can see: app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; spa.ApplicationBuilder.UseAuthentication(); if (env.IsDevelopment()) { spa.UseReactDevelopmentServer(npmScript: "start"); } }); I also added Auth controller and views with login/register endpoints. How can I add logic which will reject access to SPA untill we will

How to add reference to an external dll file in asp.net core project

一笑奈何 提交于 2019-12-11 07:58:42
问题 Everytime I try to add reference to any dll file from my non asp.net core projects to my new asp.net core project i get this error message: .NET Core projects only support referencing .NET framework assemblies in this release. To reference other assemblies, they need to be included in a NuGet package and reference that package. What should be happen here? is there a special way to do it?, seams there is something I am missing here which different than all previous asp.net version 回答1: As of

.NET Core EF Core 2 Models in 1 View Create Operation

北慕城南 提交于 2019-12-11 07:52:49
问题 Hi I have read alot but couldn't really figure out how this is suppose to work out. Following this tutorial, I couldn't quite figure out how to input data into 2 models at once with a Foreign Key mapping done properly. Supposedly I have to 2 models (following the tutorial), for example student and enrollment and I want to input data to both tables at the same time, looking at other stack overflow posts, I found out that are 2 ways to incorporate 2 views into the model, either by creating a

Can not connect to Azure SQL Server using Active directory integrated authentication in AppService

谁都会走 提交于 2019-12-11 07:34:56
问题 We have web application deploy on Azure App Service. Our database is also on Azure which is configured to use AAD authentication (We have assigned AAD Admin). We are using below connection string in web app to connect to this server and database using below connections string. Data Source=xxxxxxx.database.windows.net;Initial Catalog=xxxxxxx;Persist Security Info=False;Authentication=Active Directory Integrated Please note: This connection string is working fine when using thru local system.

ASP.NET MVC Core posting just a string to controller via Ajax Post

白昼怎懂夜的黑 提交于 2019-12-11 07:33:29
问题 Trying to send a string value to Home controller using Ajax. Following Ajax call gives the error (in Google Chrome developer tool): Failed to load resource: the server responded with a status of 404 (Not Found) : $.ajax({ url: 'Home/TestAction', data: "Test data", type: 'POST', success: function (obj) { alert('Suceeded'); } }); UPDATE : After I've tried the url part from all the responses (as of now) to this post, the url turns out to be correct but I'm still getting a warning and an error as

Controller not loading .AspNetCore.Identity.Application user cookie

感情迁移 提交于 2019-12-11 07:32:13
问题 I have a simple controller which is called after the user logs in. I can see on my identity server that the user is logged in. I can also see that .AspNetCore.Identity.Application cookie is set in the browser. After login the user is forwarded to this controller using RedirectToAction(nameof(Index), "Manage") The problem is that the controller doesn't appear to be authenticated. I have tried HttpContext.User and everything else i can think the controller isn't reading the cookie [Authorize]

MVC Core use ajax to post data and refresh a section on the page

蓝咒 提交于 2019-12-11 07:25:24
问题 I'm working on AspNet Core 2.0 MVC site. The problem is: I have 2 forms in one page, the first form is for submitting a Model, this is done via this action: [HttpPost] public IActionResult AddProductToSale([FromForm]ProductViewModel product) { ProductViewModel p = new ProductViewModel() { Id = 1, Gender = GenderType.Ambos, AvailableStock = 5, Name = "Zapatillas Nike", Size = "42", Type = ProductType.Zapatillas, UnitPrice = 1500 }; SaleViewModel.Products.Add(p); return Json(p); } the .js

How to override HandleUnauthorizedRequest in ASP.NET Core

一笑奈何 提交于 2019-12-11 06:54:40
问题 I'm migrating my project to asp.net core and I'm stuck in migrating my CustomAuthorization attribute for my controllers. Here is my code. public class CustomAuthorization : AuthorizeAttribute { public string Url { get; set; } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.Result = new RedirectResult(Url + "?returnUrl=" + filterContext.HttpContext.Request.Url.PathAndQuery); }

Error while adding AddOptions in Startup.cs

≯℡__Kan透↙ 提交于 2019-12-11 06:42:34
问题 I am trying to implement options pattern as mentioned in this link - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration#options-example When I add these 2 lines in ConfigureServices of Startup.cs public void ConfigureServices(IServiceCollection services) { // Setup options with DI services.AddOptions(); services.Configure<AppSettingsModel>(Configuration); I get below call is ambiguous between the following methods or properties error- call is ambiguous error screenshot My