编辑:添加了红隼
使用IIS express运行Asp.Net Core 2.0应用程序时,静态文件(css,js)按预期提供.但是,当通过"dotnet publish -o [targetDirectory]"和dotnet [website.dll]使用命令行/ Kestrel时,Kestrel不会提供任何静态内容.利用浏览器F12,我看到Kestrel返回404错误.仔细观察,当直接在浏览器中输入文件路径(localhost:5000/css /cssfile.css)时,文件不会显示,但浏览器似乎重定向到"localhost:5000/cssfile.css",但仍然返回404错误(注意缺少的/ css /目录).
我通过Visual Studio 2017创建了这个项目,并为新的MVC Core 2.0应用程序选择了默认值(安装了SDK).
我已按照此处的步骤在 program.cs和startup.cs文件中启用静态文件.这些实现"app.UseStaticFiles();" 和".UseContentRoot(Directory.GetCurrentDirectory())".通过谷歌找到的文章似乎都没有帮助.我已经验证了dotnet将静态内容复制到目标目录.
我错过了什么?谢谢
// Program.cs
public static IWebHost BuildWebHost(string[] args) => WebHost
.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler("/Error/HandleError");
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute( name: "default", template: "{controller=User}/{action=Index}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud) 如何在MVC Core 2.0中创建自定义授权声明(使用AspNetCore.identity)来验证自定义用户布尔属性?我已经扩展了IdentityUser(ApplicationUser)以包含一个布尔值"IsDeveloper".我正在使用基于声明的身份验证,并希望添加自定义声明,但我不确定从哪里开始.如何创建自定义声明:
我理解核心身份声称MSDN:基于声明的身份验证,但我不熟悉自定义声明,所以我不知道从哪里开始.我找到的在线文档不起作用或不适合我的场景.
claims-based-identity asp.net-identity asp.net-core-mvc asp.net-core
可能重复:
返回引用类型的方法是返回引用还是克隆副本?
我的一位同事表示,当一个方法返回一个像下面这样的对象时,会创建一个新的实例/副本,而不是传回一个引用:
public CustomerEntity Customer { get; set; }
public CustomerEntity GetCustomer() {
Customer = new CustomerEntity();
return Customer;
}
Run Code Online (Sandbox Code Playgroud)
那是对的吗?我的测试似乎表明不是这样,但我不确定如何确认这一点.他担心将数据复制到新对象的开销.
为了更好地衡量,下列哪些方法/场景是新对象创建的?调用类在哪些情况下访问原始对象的引用或副本?假设'CustomerEntity'是一个非常大的对象.
public class CustMan {
public CustomerEntity GetCustomer() {
Customer = new CustomerEntity();
return Customer
}
public void FillCustomer(CustomerEntity customer)
{
customer = new CustomerEntity();
// Calling class:
// CustomerEntity ce = new CustomerEntity();
// l_custMan.FillCustomer(ce); WriteLine(ce.Name);
}
public void LoadCustomer()
{
Customer = new CustomerEntity();
// Calling Class access customerEntity via l_custMan.CustomerEntity
}
}
Run Code Online (Sandbox Code Playgroud)
澄清:我的同事认为使用"加载"方法比使用"获取"方法更好:
l_custMan.Load();
CustomerEntity …Run Code Online (Sandbox Code Playgroud) 在之前的组织中,我们实现了一个扩展方法,该方法为String.Format创建了一个简写.该方法称为"String.F".但是我似乎无法让这个工作.一位前同事给了我以下代码,我将在下面列出我自己的测试方法.在函数'Test()'中,"String.F"抛出并出错,并且不会在intellisence中显示.我会问这是不可能的,但是我已经使用对此方法的调用来编写代码.这只是在使用实例化字符串时才有可能吗?谢谢.
public static class MyExtensions {
public static string F(this string target, params object[] args) {
return "hello";
}
}
class TestExtensions {
public string Test() {
return String.F("test:{0}", "test");
}
}
Run Code Online (Sandbox Code Playgroud)