asp.net-core-mvc

ASP.Net Core Error JSON

可紊 提交于 2019-12-12 22:21:33
问题 I'm playing a bit with ASP.NET Core. I'm creating a basic webapi. I'd like to show a JSON error when there is a problem. The printscreen shows want I want on my screen. The only problem is that it's send with a statuscode of 200. catch (NullReferenceException e) { return Json(NotFound(e.Message)); } I could solve it by doing this: return NotFound(new JsonResult(e.Message) {StatusCode = 404); But I don't like this because now you can specify statuscode 500 with a NotFound. Can someone put me

ASP.NET Core 2 : Ajax calls to API with [Authorize] fail the preflight request

拥有回忆 提交于 2019-12-12 22:21:17
问题 I have an API controller with the [Authorize] attribute to ensure all calls are made by properly logged-in users (via OpenIdConnect, targeting Azure AD) . When I load the page that makes the Ajax calls, the API responds properly. However, after a certain amount of time, the user has to be reauthorized, and thus a call to https://login.microsoftonline.com/ is made by the ASP.NET Core framework to reauth the user. That calls fails with the following error: Failed to load https://login

Browser Link not working with Web Application from ASP.NET 5 Templates

扶醉桌前 提交于 2019-12-12 20:44:43
问题 I have Visual Studio Community 2015 with Update 1 installed in my Windows 8.1 machine. When I try to create and run new "Project" (File->New->Project->MVC) under ASP.NET 4.5.2 Templates I find Refresh Linked Browsers (Ctrl+Alt+Enter) option working and enabled. While trying to create and run new "Project" (File->New->Project->Web Application) under ASP.NET 5 Templates I find Refresh Linked Browsers option disabled. Is there something wrong with new version or browser link is not supported by

MVC6 attribute routing with two parameters

前提是你 提交于 2019-12-12 20:39:58
问题 I've had a look around for this and nothing that pertains to the MVC6 taghelper anchor tag in relation to having an alternative [HttpGet] method that caters for multiple parameters. Sure you can add multiple parameters to a MVC6 anchor taghelper but how do you process the second option with two parameters using attrubute routing... I have two [HttpGet] IactionResult methods: //GET: UserAdmin public async Task<IActionResult> Index() { return View(await _userAdminService.GetAllUsers("name_desc"

in ASP.NET Core MVC, is it possible to add a View folder from outside the project folder?

徘徊边缘 提交于 2019-12-12 19:24:29
问题 The majority of my views are regular <action>.cshtml files in the normal /Views/<Controller> folder hierarchy. These are source controlled in git and deployed in the usual "rip and replace" way. However I also use Razor for rendering templates to create HTML emails, and the email .cshtml templates are specific to each client. I therefore want to be able to load and render them from outside the Application Root folder, so the client-specific customisations are not lost during deployment. I

Command equivalent to AntiForgery.Validate() in asp.net-core

孤者浪人 提交于 2019-12-12 18:23:40
问题 Exists, in asp.net-core, a command similar to AntiForgery.Validate(); to validate antiforgery token in the action body? Code example in dotnet 4.5.1: public ActionResult Index() { if (!User.Identity.IsAuthenticated) { System.Web.Helpers.AntiForgery.Validate(); } // rest of action } 回答1: Antiforgery token validation can be done automatically using filter attributes in your controllers. Use [AutoValidateAntiforgeryToken] to validate the token on all "unsafe" methods. (Methods other than GET,

How to format Date, Time, Number in ASP.NET Core 2.0, RAZOR Tag Helpers

自古美人都是妖i 提交于 2019-12-12 17:49:27
问题 I'm facing problems trying to format DateTime and Numbers in ASP.NET MVC Core 2.0 App using Razor Tag Helpers. Following the good practices I would like to specify data format at the data model related to the form view. My Model public class OrderFormModel { public bool IsEdit { get; set; } [Display(Name = "Order Time: ")] public DateTime ORDER_LOCAL_TIME { get; set; } [Display(Name = "Order Qty (K): ")] public int QUANTITY_K { get; set; } The view: <div class="form-group col-md-9"> <label

Common wwwroot folder for multiple websites in MVC 6

老子叫甜甜 提交于 2019-12-12 16:33:23
问题 I have multiple websites which use /wwwroot/assets folder (html theme, css and javascript files) to load the static content. Currently I am copying assets folder in each site. All of my projects are sitting under a common parent directory. I don't want to copy the /wwwroot/assets folder into each website. Is there a way to share one assets folder between all sites. May be by providing a direct file system path or something? 回答1: At the moment it's not clear from the documentation what sorts

How do I create Constructor Subscription in vNext Dependency Injection

时光毁灭记忆、已成空白 提交于 2019-12-12 16:17:43
问题 I am experimenting with a throwaway vNext application. What I'm trying to do is create a dummy class called 'DataService' which will return some data, and then create objects which use DI to take an IDataService parameter as the constructor. So my IDataService/DataService definitions are: public interface IDataService { DateTime Created { get; } } public class DataService : IDataService { private DateTime created = DateTime.Now; public DateTime Created { get { return created; } } } In my

Which encryption algorithm do we use in Asp.net 5 Core

女生的网名这么多〃 提交于 2019-12-12 15:26:53
问题 Currently almost all of the encryption algorithms start giving me errors when I switch the project type from Asp.net 5.0 to Asp.net 5.0 Core. This is because they all require System.Security.Cryptography namespance, which is not available in Core 5.0. See some code examples here. Any idea which one we could use in Asp.net core 5.0? 回答1: You should use the Microsoft.AspNet.Security.DataProtection package. The 2 main interfaces you can use are: https://github.com/aspnet/DataProtection/blob/dev