在MVC中,用户可以使用如下操作过滤器:
[Log]
ActionResult Home()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
现在,我将在asp.net Web表单中找到与ActionFilter等效的文件。我怎样才能像上面那样为asp.net Web表单模仿并将它们应用于事件处理程序?
[Log]
protect void btnSearch_Click(object sender, EventArgs e)
{
//Do some operations here
}
Run Code Online (Sandbox Code Playgroud)
更新:
为了澄清我的问题(正如评论所说,我的问题有点含糊),我想记录方法的开始和方法的结束。在Mvc中,我可以这样创建Action Filter:
public class LogAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//Logging start of method.
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//Logging end of method.
}
}
Run Code Online (Sandbox Code Playgroud) 我将使用Professional ASP.NET Design Patterns学习 MVP模式。在表示层章节中,它学习了如何将 MVP 应用于 asp.net。演示者的代码是:
public class HomePagePresenter : IHomePagePresenter
{
private IHomeView _view;
private ProductService _productService;
public HomePagePresenter(IHomeView view, ProductService productService)
{
_productService = productService;
_view = view;
}
public void Display()
{
_view.TopSellingProduct = _productService.GetBestSellingProducts();
_view.CategoryList = _productService.GetAllCategories();
}
}
public interface IHomePagePresenter
{
void Display();
}
Run Code Online (Sandbox Code Playgroud)
作者说:
我已经定义了这个(HomePagePresenter 的接口)来松散耦合代码并帮助测试。
我不明白他将如何使用演示者界面来创建测试?当我查看nmock 示例时,他们也没有为演示者创建任何界面。
我想使用FluentValidation验证我的域模型实体。我已经阅读了有关DDD验证的答案,该答案已使用FluentValidation验证其实体。这是他如何验证其实体的方法:
public class ParticipantValidator : AbstractValidator<Participant>
{
public ParticipantValidator(DateTime today, int ageLimit, List<string> validCompanyCodes, /*any other stuff you need*/)
{...}
public void BuildRules()
{
RuleFor(participant => participant.DateOfBirth)
.NotNull()
.LessThan(m_today.AddYears(m_ageLimit*-1))
.WithMessage(string.Format("Participant must be older than {0} years of age.", m_ageLimit));
RuleFor(participant => participant.Address)
.NotNull()
.SetValidator(new AddressValidator());
RuleFor(participant => participant.Email)
.NotEmpty()
.EmailAddress();
...
}
}
Run Code Online (Sandbox Code Playgroud)
因此,我的域项目依赖FluentValidation库。
但是我认为我的域项目依赖第三方库是一个不好的主意。我如何预防这个问题?
c# validation design-patterns domain-driven-design fluentvalidation
我将为我的 Web 项目创建设置。我使用http://blog.bartdemeyer.be/2013/10/create-an-installer-for-website-with-wix-part-1/作为我的参考。在文章中间,作者创建了一个名为WebSiteContent.wxsusing heat.exe的文件:
<Target Name="Harvest">
<!-- Harvest all content of published result -->
<Exec
Command='$(WixPath)heat dir $(Publish) -dr INSTALLFOLDER -ke -srd -cg MyWebWebComponents -var var.publishDir -gg -out $(WebSiteContentCode)'
ContinueOnError="false"
WorkingDirectory="." />
</Target>
Run Code Online (Sandbox Code Playgroud)
运行 msbuild 后,文件包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<!--...-->
<Fragment>
<ComponentGroup Id="MyWebWebComponents">
<!--...-->
<Component Id="cmpCDB7F4EFDEF1E65C3B12BEBAD7E4D7EA" Directory="INSTALLFOLDER" Guid="{7EA5DB39-513D-482B-9FDC-2F16FCE5E712}">
<File Id="fil8994620207C22CA15AF75ACDD6420C79" KeyPath="yes" Source="$(var.publishDir)\Web.config" />
</Component>
</ComponentGroup>
<!--...-->
</Fragment>
</Wix>
Run Code Online (Sandbox Code Playgroud)
我想更改 web.config 文件内容的值,如从 WiX 更改 XML 节点值中所述,但我不知道如何将 WebSiteContent.wxs 文件之外的引用添加到 fil8994620207C22CA15AF75ACDD6420C79 元素。
我知道我可以将 xml 脚本添加到 …
Angularjs使用非常精简版本jQuery的叫jQLite(或mini-jQuery在一些网站).如果你添加对主jQuery的引用,Angularjs将使用你的主jQuery,我总是在我的应用程序中引用主jQuery.因此,为了减小Angularjs的大小,我想从Angularjs文件中删除jQLite.什么是简单(和安全)的方式来做到这一点?
Person我有带有以下字段的表(以及实体数据模型中的实体) :
Name Type
SocialID String
FirstName String
LastName String
Run Code Online (Sandbox Code Playgroud)
这SocialID是主键。我想更新SocialID每条记录的值。但是,当我尝试在实体框架中更新此字段时,出现以下错误:
The property 'SocialID' is part of the object's key information and cannot
be modified.
Run Code Online (Sandbox Code Playgroud)
我得到上述错误的代码是:
foreach (var p in Entity.Persons)
{
p.SocialID= p.SocialID + "00";
Entity.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
<div id="a">ONE</div>
<div id="b">TWO</div>
<div id="c">THREE</div>
var el = document.getElementsByTagName("div");
for ( var i = 0; i < el.length; i++ ) (function(){
el.addEventListener("click",function(){
console.log(this.id + " " + this.innerHTML);
},false);
})(i);
Run Code Online (Sandbox Code Playgroud)
给我错误"el.addEventListener不是函数",有什么问题?
我想使用其sparql端点从dbpedia下载一些实体配置文件.我的查询是:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia: <http://dbpedia.org/ontology/>
select ?x where {?x rdf:type dbpedia:Drug} LIMIT 100
Run Code Online (Sandbox Code Playgroud)
上述查询的结果是实体配置文件的一些链接.对于解除引用,我必须单击每个链接.我想取消引用所有实体配置文件并将其另存为本地计算机中的数据集.我想稍后在我的项目中使用这个数据集.那么我如何下载这个实体配置文件?有sparql命令吗?
我创建了一个新的 ASP.NET Core Web API 项目。这是ConfigureServices在 Startup.cs 中:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddMemoryCache();
var serviceProvider = services.BuildServiceProvider();
var cache = serviceProvider.GetService<IMemoryCache>();
cache.Set("key1", "value1");
//_cahce.Count is 1
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我向 IMemoryCache 添加了一个项目。这是我的控制器:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly IMemoryCache _cache;
public ValuesController(IMemoryCache cache)
{
_cache = cache;
}
[HttpGet("{key}")]
public ActionResult<string> Get(string key)
{
//_cahce.Count is 0
if(!_cache.TryGetValue(key, out var value))
{
return NotFound($"The value with the {key} is not found");
}
return value …Run Code Online (Sandbox Code Playgroud) 我知道何时在C#中使用锁定块,但我不清楚的是lock variable在lock语句的括号中.请考虑Singleton Design Pattern从DoFactory进行以下操作:
class LoadBalancer
{
private static LoadBalancer _instance;
private List<string> _servers = new List<string>();
private static object syncLock = new object();
public static LoadBalancer GetLoadBalancer()
{
// Support multithreaded applications through
// 'Double checked locking' pattern which (once
// the instance exists) avoids locking each
// time the method is invoked
if (_instance == null)
{
lock (syncLock)
{
if (_instance == null)
{
_instance = new LoadBalancer();
} …Run Code Online (Sandbox Code Playgroud) 什么是DbSet的扩展名?我想添加'FindTheLatest'方法.这是我的代码:这是我的代码
public static List FindTheLatest(this DbSet<Review> reviews, int nums)
{
return reviews.OrderByDescending(r => r.Created).Take(nums).ToList();
}
但它不起作用.我该怎么办?
这是我的扩展类:
public static class RestaurantReviewsExtenstion
{
public static IEnumerable<Review> FindTheLatest(this IQueryable<Review> reviews, int nums)
{
return reviews.OrderByDescending(r => r.Created).Take(nums).ToList();
}
public static Review FindById(this System.Data.Entity.DbSet<Review> reviews, int id)
{
return reviews.SingleOrDefault(s => s.ReviewId == id);
//return reviews.Find(item => item.Id == id);
}
public static Review FindTheBest(this List<Review> list)
{
return list.OrderByDescending(o => o.Rating).FirstOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的DBContex类:
public class OdeToFoodDb: DbContext
{
public DbSet<Restaurant> Restaurants …Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net ×3
javascript ×2
.net-core ×1
angularjs ×1
asp.net-core ×1
asp.net-mvc ×1
function ×1
heat ×1
jqlite ×1
jquery ×1
msbuild ×1
mvp ×1
owl ×1
rdf ×1
semantic-web ×1
sparql ×1
validation ×1
winforms ×1
wix ×1