我正在尝试将我的 ASP.NET Core 2.0 Web 应用程序全球化并本地化为西班牙语。我正在尝试遵循文档,但这些文档似乎并不适用于 Core 2.0,因为它们似乎没有涵盖 Razor Pages。仅控制器和视图。然而,无论我更改请求标头中的区域性还是使用文档中所示的查询字符串,本地化都不起作用。对我做错了什么有任何见解吗?
http://localhost:26417/?culture=es&ui-culture=es
Run Code Online (Sandbox Code Playgroud)
启动.cs
public void ConfigureServices( IServiceCollection services )
{
services.AddDbContext<PartDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("PartDatabase"))
);
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddLocalization(options => options.ResourcesPath = "Localization");
services.AddMvc()
.AddRazorPagesOptions(options => {
//options.Conventions.AllowAnonymousToPage("/Index");
// I can just use [AllowAnonymous] attribute
})
.AddViewLocalization()
.AddDataAnnotationsLocalization();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure( IApplicationBuilder app, IHostingEnvironment env )
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
var …Run Code Online (Sandbox Code Playgroud) 当我提交表单时,ModelValidation.IsValid 为 true,但我使用的 [BindProperty] 对象为 null。
页面模型
[BindProperty]我在房产上有标签public Group Group {get; private set}。
在该OnGetAsync(int? Id)方法中,我查找组并.FindAsync(Id)填充表单。效果很好:)
我的理解是,OnPostAsync()由于 BindProperty 注释,应该自动填充 Group 对象。但是,一旦我发布 ModelState 有效,但 Group 对象为空。我该如何解决?
using xxx.ReportGroups.Data;
using xxx.ReportGroups.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xxx.ReportGroups.Pages
{
public class GroupEditModel : PageModel
{
private readonly ApplicationDbContext _db;
public GroupEditModel(ApplicationDbContext db)
{
_db = db;
}
[BindProperty]
public string ErrorMessage { get; …Run Code Online (Sandbox Code Playgroud) 寻找一种简单的方法构建多租户剃刀页面。寻找\{Tenant}\{Page}与某个区域中所有页面相似的 url 模式。通过 RazorPagesOptions Conventions 在末尾添加路由参数相当容易。如何在开头添加参数?
我正在设置一个新的 Razor Pages 应用程序,并且想要添加基于角色的授权。网络上有很多教程如何使用 ASP.NET MVC 应用程序执行此操作,但没有使用 Razor 页面。我尝试了一些解决方案,但对我来说没有任何作用。目前我有一个问题,如何为数据库添加角色并将该角色添加到每个新注册用户。
这就是我的样子Startup.cs:
public async Task ConfigureServices(IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(config =>
{
config.SignIn.RequireConfirmedEmail = true;
})
.AddRoles<IdentityRole>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddAuthorization(config =>
{
config.AddPolicy("RequireAdministratorRole",
policy => policy.RequireRole("Administrator"));
});
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
services.AddRazorPages()
.AddNewtonsoftJson()
.AddRazorPagesOptions(options => {
options.Conventions.AuthorizePage("/Privacy", "Administrator");
}); …Run Code Online (Sandbox Code Playgroud) 我有一个 Razor Pages 应用程序,其中站点中的所有页面都使用多项服务BasePageModel,并将这些服务添加到我在每个 Razor 页面上继承的 。我的BasePageModel看起来像这样:
public abstract class BasePageModel : PageModel
{
private IUserClaimsService _userClaims;
private IAuthorizationService _authService;
protected virtual IUserClaimsService UserClaimsService => _userClaims ?? (_userClaims = HttpContext.RequestServices.GetService<IUserClaimsService>());
protected virtual IAuthorizationService AuthService => _authService ?? (_authService = HttpContext.RequestServices.GetService<IAuthorizationService>());
}
Run Code Online (Sandbox Code Playgroud)
Razor 页面本身看起来像这样:
public class IndexModel : BasePageModel
{
public async Task<ActionResult> OnGet()
{
var HasAccess = (await AuthService.AuthorizeAsync(User, "PermissionName")).Succeeded;
if (!HasAccess)
{
return new ForbidResult();
}
return Page();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在创建单元测试来测试 Razor 页面上的授权。我的授权具有取决于用户声明的策略。我想做的是模拟用户声明并相应地测试授权是否成功或失败,具体取决于页面。但是,我无法UserClaimsService从我的测试中模拟它。我实例化了 …
在 Asp.Net core 中,我有一个 razor 页面,我想将 Ajax 帖子发送到 Post 方法,但我总是得到 null 模型。这是我的简化问题。
public class IndexModel : PageModel
{
public void OnPost([FromBody]A A)
{
if (ModelState.IsValid)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模型:
[JsonObject(MemberSerialization.OptOut)]
public class A
{
[JsonProperty]
public string Id { get; set; }
[JsonProperty]
public string CityId { get; set; }
[JsonProperty]
public string Infected { get; set; }
[JsonProperty]
public string Susceptible { get; set; }
[JsonProperty]
public string Recovered { get; set; }
[JsonProperty]
public string CityName { …Run Code Online (Sandbox Code Playgroud) 如何使用 Razor 在 ASP.NET Core 2.2 中根据复选框状态显示或隐藏 div 元素?
我有这个,但它不起作用:
<script>
$(function() {
$('#gridCheck1').change(function() {
$('#ShowHideMe').toggle($(this).is(':checked'));
});
});
</script>
<div class="form-group row">
<div class="col-sm-2">Checkbox</div>
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Example checkbox
</label>
</div>
</div>
</div>
<div id="ShowHideMe">
<p>some content</p>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的项目文件夹中的库:
我正在使用 asp.net 核心 2.1。
我的主页在 Pages 文件夹下,我的部分视图 _test2 位于共享文件夹中。这是我的主页:
public class MainModel : PageModel
{
public IRepositoryStudent irep;
public MainModel (IRepositoryStudent _irep){
irep = _irep;
}
[BindProperty]
public Student student{ get; set; }
public void OnGet()
{
student = irep.GetFirst();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 Main.cshtml:
@page
@model APWeb.Pages.MainModel
<h2>Main</h2>
<partial name="_test2" model="Model.student" />
Run Code Online (Sandbox Code Playgroud)
这是我的部分观点:_test2:
@page
@*
For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
@model Student
@{
<h1 >@Model.Name</h1>}
Run Code Online (Sandbox Code Playgroud)
我在局部视图中的模型为空,并且出现此错误:
NullReferenceException: Object reference not set to an …Run Code Online (Sandbox Code Playgroud) partial-views asp.net-mvc-partialview asp.net-core razor-pages asp.net-core-2.1
为我的 ASP.NET Core Razor Pages 应用程序创建的默认启动代码包括以下代码:
app.UseHttpsRedirection();
Run Code Online (Sandbox Code Playgroud)
这似乎是碰巧了。现在,在我的开发计算机上,编辑地址栏中的 URL 以使用 HTTP 而不是 HTTPS 会出现“连接已重置”错误。
另外,我找到了该AddRedirectToHttpsPermanent()选项,可以将其传递给app.UseRewriter().
此时,我不清楚为什么app.UseHttpsRedirection()似乎不起作用,或者我是否应该使用UseRewriter().
有没有人弄清楚这一点?
嗨,我正在尝试制作预订页面。如果有人进行预订,该日期会保存在数据库中,并且也会显示在他们的页面上。'dayid' 列的类型是 postgresql 中的日期。在剃刀页面 C# 中,我将 DateTime 类型用于变量 Dayid。我需要将 dayid 值从数据库转换为字符串。但我不知道如何解决这个错误:“方法'ToString'没有重载需要1个参数”这是代码
public List<ReservationModel> ShowReservation()
{
var cs = Database.Database.Connector();
List<ReservationModel> res = new List<ReservationModel>();
using var con = new NpgsqlConnection(cs);
{
string query = "Select dayid, locationid FROM reservation";
using NpgsqlCommand cmd = new NpgsqlCommand(query, con);
{
cmd.Connection = con;
con.Open();
using (NpgsqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
res.Add(new ReservationModel { Dayid = dr["dayid"].ToString("MM/dd/yyyy"), Locationid = dr["locationid"].ToString() });
}
}
con.Close();
}
}
return res;
}
Run Code Online (Sandbox Code Playgroud) razor-pages ×10
c# ×6
asp.net-core ×5
.net-core ×1
ajax ×1
asp.net ×1
asp.net-mvc ×1
checkbox ×1
identity ×1
javascript ×1
json ×1
localization ×1
postgresql ×1
roles ×1
unit-testing ×1