asp.net-core-mvc

ASP.NET Core Identity does not inject UserManager<ApplicationUser>

蓝咒 提交于 2020-01-12 14:26:07
问题 I've got an older asp.net core identity database, and I want to map a new project (a web api) to it. Just for the test, I copied the Models folder, and the ApplicationUser file from the previous project (ApplicationUser simply inherits from IdentityUser, no changes whatsoever) - doing DB first seems to be a bad idea. I'm registering Identity in ConfigureServices (but I'm not adding it to the pipeline since my only intention is to use the UserStore) services.AddIdentity<ApplicationUser,

'HttpPostedFileBase' in Asp.Net Core 2.0

我与影子孤独终老i 提交于 2020-01-12 14:11:31
问题 I'm recently working on a ReactJS app that's calling an API (developed with .NET Core 2.0). My question is how to use HttpPostedFileBase in an .NET Core 2.0 API in order to get file content and save it in database. 回答1: You don't have HttpPostedFileBase in ASP.NET Core 2.0, but you can use IFormFile . [HttpPost("UploadFiles")] public async Task<IActionResult> Post(List<IFormFile> files) { long size = files.Sum(f => f.Length); // full path to file in temp location var filePath = Path

Localization in ASP.Net core MVC not working - unable to locate resource file

霸气de小男生 提交于 2020-01-12 12:54:08
问题 In trying to localize my application, I've followed the steps here: https://docs.asp.net/en/latest/fundamentals/localization.html Here is my code: Startup.cs public List<IRequestCultureProvider> RequestCultureProviders { get; private set; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddLocalization(options => options.ResourcesPath = "Resources"); services.AddMvc()

ASP.NET Core - Custom model validation

有些话、适合烂在心里 提交于 2020-01-12 06:44:12
问题 In MVC when we post a model to an action we do the following in order to validate the model against the data annotation of that model: if (ModelState.IsValid) If we mark a property as [Required], the ModelState.IsValid will validate that property if contains a value or not. My question: How can I manually build and run custom validator? P.S. I am talking about backend validator only. 回答1: In .NET Core, you can simply create a class that inherits from ValidationAttribute . You can see the full

Set Default/Null Value with Select TagHelper

馋奶兔 提交于 2020-01-12 02:55:09
问题 In asp.net mvc you can use: @Html.DropDownListFor(model => model.Category, ViewBag.Category as IEnumerable<SelectListItem>, "-- SELECT --", new { @class = "form-control" }) Using asp.net 5, how do I include the default or null value (-- SELECT --) in a taghelper: <select asp-for="Category" asp-items="@ViewBag.Category" class="form-control"></select> 回答1: You can just insert an option item inside the select: <select asp-for="Category" asp-items="@ViewBag.Category" class="form-control"> <option

How to avoid calling ModelState.IsValid on every PostBack?

泪湿孤枕 提交于 2020-01-11 11:25:12
问题 I pretty much always want to check if ModelSate.IsValid is called when I do a postback. And having to check at the start of every post back violates the DRY principle, is there a way to have it checked automatically? Example: [HttpPost("RegisterUser")] [AllowAnonymous] public async Task<IActionResult> RegisterUser([FromBody] UserRegisterViewModel vmodel) { if(!ModelState.IsValid) // This code is repeated at every postback return ModelInvalidAction(); // Is there a way to avoid having to write

How to use IApplicationBuilder middleware overload in ASP.NET Core

偶尔善良 提交于 2020-01-11 09:16:29
问题 ASP.NET Core provides two overloads for app.Use() method. Usually we use only one overload that is app.Use(Func<HttpContext,Func<Task>, Task> middleware) Which is used as app.Use(async (context, next) => { await context.Response.WriteAsync("1st middleware <br/>"); await next.Invoke(); }); The other overload that i want to use is app.Use(Func<RequestDelegate,RequestDelegate> middleware) I couldn't find an example of how we can use this overload. Any ideas will be great. 回答1: Func

Where is the site.css file located for Identity?

那年仲夏 提交于 2020-01-11 07:25:17
问题 I have an asp.net core 2.1 MVC application. I have ran the Scaffold Identity which has generated all the HTML and models used. I can't however find the css file that identity is using for its layouts? In chrome developer tools it tells me site.css is being loaded from /Identity/css/site.css and bootstrap is being loaded from /Identity/lib/bootstrap/dist/css/bootstrap.css . These files don't seem to exist anywhere in my project. Am I missing something? It's not using the site.css file that's

How to transform XML to HTML with XSLT in C#?

[亡魂溺海] 提交于 2020-01-11 07:20:12
问题 How can I transform XML to HTML with XSLT in ASP.NET Core? I thought about: public static string TransformXMLToHTML(string inputXml, string xsltString) { XslCompiledTransform transform = new XslCompiledTransform(); using(XmlReader reader = XmlReader.Create(new StringReader(xsltString))) { transform.Load(reader); } StringWriter results = new StringWriter(); using(XmlReader reader = XmlReader.Create(new StringReader(inputXml))) { transform.Transform(reader, null, results); } return results

How to transform XML to HTML with XSLT in C#?

会有一股神秘感。 提交于 2020-01-11 07:20:10
问题 How can I transform XML to HTML with XSLT in ASP.NET Core? I thought about: public static string TransformXMLToHTML(string inputXml, string xsltString) { XslCompiledTransform transform = new XslCompiledTransform(); using(XmlReader reader = XmlReader.Create(new StringReader(xsltString))) { transform.Load(reader); } StringWriter results = new StringWriter(); using(XmlReader reader = XmlReader.Create(new StringReader(inputXml))) { transform.Transform(reader, null, results); } return results