是什么之间的区别customErrors,并httpErrors在ASP.NET MVC应用程序的web.config文件的部分?
使用每个部分的准则是什么?
我有一个场景,我要求用户能够使用Windows身份验证或Forms身份验证对ASP.NET MVC Web应用程序进行身份验证.如果用户在内部网络上,他们将使用Windows身份验证,如果他们在外部连接,他们将使用Forms身份验证.我见过很多人问这个问题我如何为此配置ASP.NET MVC Web应用程序,但我还没有找到完整的解释.
有人可以通过代码示例提供有关如何完成此操作的详细说明吗?
谢谢.
艾伦T.
我使用以下方法创建了一个测试项目:
dotnet new razor --auth Individual --output Test
Run Code Online (Sandbox Code Playgroud)
这将创建一个Startup.cs,其中包含:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Run Code Online (Sandbox Code Playgroud)
我想为一些用户和角色提供种子.用户和角色都将使用同一个商店(SQLite).我正在使用一个静态类来播种它Program.
我可以播种用户,但不能播放角色,因为上面似乎没有注入RoleManager.
在ASP.NET Core 2.0中使用以下内容:
services.AddIdentity<IdentityUser, IdentityRole>()
Run Code Online (Sandbox Code Playgroud)
我猜测AddDefaultIdentity2.1是新的,但问题是它没有注入RoleMnager,所以我该怎么办?
如果有人可以发布一个示例nlog.config以便在SQL Server Compact 4.0中使用nlog,我将不胜感激.
我可以输出到控制台和文件确定.我已经尝试了各种dbProviders和connectionStrings,但似乎没有任何工作.
提前致谢.
艾伦T.
我有一个奇怪的问题,即ValidationSummary没有显示.但是,正在显示ValidationMessage.我检查了输出页面源,并不是说它们的颜色模糊了它们.我正在使用RC.有任何想法吗?
编辑:ValidationSummary设置的断点显示:
ViewData.ModelState.Values[1].ErrorMessage = ""
ViewData.ModelState.Values[1].Exception.InnerException.Message = "4a is not a valid value for Int32"
Run Code Online (Sandbox Code Playgroud)
ValidationSummary是否使用ErrorMessage和ValidationMessage使用InnerException.Message?
我的观看代码是:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<App.Models.PurchaseOrdersView>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<title>Edit</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<%= Html.ValidationSummary() %>
<% Html.BeginForm("Edit", "PurchaseOrder", FormMethod.Post); %>
<table>
<tr>
<td>
Purchase Order Id:
</td>
<td>
<%= Html.TextBox("PurchaseOrderId", Model.PurchaseOrderId)%>
<%= Html.ValidationMessage("PurchaseOrderId")%>
</td>
</tr>
<tr>
<td>
Date:
</td>
<td>
<%= Html.TextBox("Date", Model.Date.ToString("dd-MMM-yyyy"))%>
<%= Html.ValidationMessage("Date")%>
</td>
</tr>
</table>
<input type="submit" value="Save" />
<% Html.EndForm(); %>
</asp:Content>
Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.NET MVC的RC1.
我正在尝试扩展Phil Haack的模型绑定示例.我正在尝试使用默认模型绑定器来绑定以下对象:
public class ListOfProducts
{
public int Id { get; set; }
public string Title{ get; set; }
List<Product> Items { get; set; }
}
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Phil的示例中的代码进行一些更改:
控制器:
using System.Collections.Generic;
using System.Web.Mvc;
namespace TestBinding.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
//Action method …Run Code Online (Sandbox Code Playgroud) 我对 F# 很陌生,我正在尝试创建一个带参数的控制台应用程序。我找到了Argu库,并一直试图让一个基本的例子工作。如果通过--commanda或--commandb参数,以下内容将作为预期工作,但如果我尝试,则会出现异常--help。
open System
open Argu
type CliArguments =
| CommandA
| CommandB
with
interface IArgParserTemplate with
member s.Usage =
match s with
| CommandA -> "CommandA - Do something"
| CommandB -> "CommandB - Do something"
[<EntryPoint>]
let main argv =
let parser = ArgumentParser.Create<CliArguments>()
let results = parser.Parse argv
results.GetAllResults()
|> List.iter (fun x ->
match x with
| CommandA -> printfn "CommandA"
| CommandB -> printfn "CommandB")
0 …Run Code Online (Sandbox Code Playgroud) 我正在使用Pester对我编写的某些代码进行单元测试。在测试中,我Test-Path使用参数过滤器进行了模拟:
Mock -CommandName 'Test-Path' -MockWith { return $false } `
-ParameterFilter { $LiteralPath -and $LiteralPath -eq 'c:\dummy.txt' }
Run Code Online (Sandbox Code Playgroud)
以下是我正在做的伪代码:
If ( Test-Path -LiteralPath c:\dummy.txt )
{
return "Exists"
}
else
{
Attempt to get file
If ( Test-Path -LiteralPath c:\dummy.txt )
{
return "File obtained"
}
}
Run Code Online (Sandbox Code Playgroud)
在第一次通话中Test-Path我想返回$false,在第二个通话中我想返回$true。我可以想到几种实现此目的的方法,但是它们感觉不正确:
第一次调用时使用Path参数,第二次使用LiteralPath。有两个模拟ParameterFilter,每个模拟一个。我不喜欢为了方便测试而修改代码的想法。
创建具有以下参数的函数:Path和InstanceNumber。为该函数创建模拟。这比上面的要好,但是我不喜欢仅用于测试目的的参数的想法。
我已经看过,但找不到基于第n个调用进行模拟的方法。佩斯特(Pester)对此有帮助吗,我刚刚错过了吗?如果没有,有没有一种更清洁的方式来实现我想要的?
我的Razor Pages应用程序配置如下.Startup.cs包含:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdminRole", policy =>
policy.RequireAuthenticatedUser().RequireRole("Admin"));
});
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizePage("/About", "RequireAdminRole");
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy(); …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×3
asp.net-core ×2
c# ×2
.net ×1
asp.net ×1
binding ×1
f# ×1
f#-argu ×1
iis-7.5 ×1
mixed-mode ×1
nlog ×1
pester ×1
powershell ×1
unit-testing ×1
validation ×1
web-config ×1