razor-pages

How to retrieve a service in razor pages with dependency injection

孤街醉人 提交于 2019-12-12 19:17:17
问题 In ASP.NET Core 2 application I set up some services: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyContext"))); services.AddHangfire(options => options.UseSqlServerStorage(Configuration.GetConnectionString("MyContext"))); services.AddOptions(); services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options)); services.AddMvc()

Razor page link ignores route argument

痴心易碎 提交于 2019-12-12 10:45:43
问题 I can't believe how frustratingly difficult I'm finding such a simple task. I want to create a link with a page query argument. I tried the following: <a asp-page="Index" asp-route-page="10">Character Classifications</a> And here's the link that is generated. <a href="/">Character Classifications</a> I've been Googling for an hour. Can anyone tell me the Razor Pages way to create a link with a query argument? Also, is there a shortcut if I'm linking to the current page? 回答1: Kirk's answer is

SignInManager.PasswordSignInAsync() succeeds, but User.Identity.IsAuthenticated is false

依然范特西╮ 提交于 2019-12-12 09:07:20
问题 I'm new to ASP.Net Core and trying to create an user authentication system. I'm using ASP.Net Core Identity user management. I have the below code for logging in an user. /Areas/Identity/Pages/Account/Login.cshtml.cs public async Task<IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set

How to bind a property to a dynamic list of objects

▼魔方 西西 提交于 2019-12-11 19:03:40
问题 I am using the .net core 2.2 framework and am having trouble finding out how I can bind a list of objects to a razor page. On the razor page I have a button to add a new row to a table. This button executes a jquery click event that alters the html to add a new row without refreshing the page. What I am looking to do with this, is that when I add a row it binds it to the list of objects. I can then process this list of items when I post the form. My Razor Page cs looks something like this:

Custom 404 Page Doesn't Get Redirected

泄露秘密 提交于 2019-12-11 18:04:08
问题 I am trying to redirect to 404 custom error page whenever a user types wrong url in the browser. Say a user types https://localhost:44332/foo (foo page doesn't exist), then it should redirect to the custom error page. In my case, it doesn't, but for this, it works: https://localhost:44332/foo/foo-page This get redirected to custom error page. I did a small workout in my project and believe, this causes an issue for this redirection - https://localhost:44332/foo . I've a page, say user-details

ASP Core: how to configure area for api controller without AreaAttribute (or how to enable convention area routing for Api controller)?

99封情书 提交于 2019-12-11 17:56:31
问题 Razor Pages do not need any AreaAttribute and it is enough to place them into Areas/MyArea/Pages folder and this is enough to enable routing /MyArea/MyPage + My Proj + Areas + MyArea + Pages - MyPage.cshtml But when if I put API Controlller MyApi.cs to MyArea folder: + My Proj + Areas + MyArea + Pages - MyApi.cs then I am become forced to "hardcode" area name [Area("MyArea")] // !!! can't be missed [Route("[area]/api/[action]")] [Produces("application/json")] [ApiController] public class

How to load partial razor page into razor view

♀尐吖头ヾ 提交于 2019-12-11 17:22:21
问题 I am trying to replace my view components with razor pages but it seems that it's not possible to load a partial razor page because a model is expected to be passed yet it is my understanding that the model for a razor page should be declared in the OnGetAsync method. Here is my code... Razor Page @page "{id:int}" @model _BackgroundModel <form method="POST"> <div>Name: <input asp-for="Description" /></div> <input type="submit" /> </form> Razor Page Code-Behind public class _BackgroundModel :

Url.Action in asp.net core

╄→гoц情女王★ 提交于 2019-12-11 16:20:48
问题 I like the new tag helpers attributes which make readable dynamic html contents. I can create a link by using the asp-controller and asp-action attributes like this: <a asp-controller="Home" asp-action="Index">Index</a> The problem is that there are cases where I need just the URL of the action in order to use it my javascript. For example in order to make an ajax call in ASP.NET MVC 5, I could have the following code: $.ajax({ type: "POST", url: '@Url.Action("GetData","Ajax")', data: "{ }",

Using remote attribute in .net core razor pages. The Parameter does not get value in Action Method of Controller

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 15:13:46
问题 I am using Remote Attribute validation The method is invoked successfully on textchange event. However, the parameters in the action method of the controller does not get the value of the field. Here is the Action Method in the HomeController.cs. When invoked the Name parameter remains null . I will be pleased if someone solve this problem [AcceptVerbs("Get", "Post")] public async Task<ActionResult> IsExist(string Name) { List<Keywords> keywords = new List<Keywords>(); HttpClient client =

How do I call a function or a method on a select list if the value changes?

只谈情不闲聊 提交于 2019-12-11 15:12:18
问题 I have a scenario where if I change the value of a select list I want to be able to clear the model or any values I want to clear. Is it possible to call a function or a method in a select tag helper? Something like this: <select asp-for="Input.DriverName" class="form-control" asp-items="Model.DriverNotUser" onchange="myfunction()"> </select> 回答1: To call a javascript function : <select class="form-control" asp-for="DriverName" asp-items="Model.DriverNotUser" onchange="myfunction();"> <