我有以下课程
SearchQuery<TEntity>
Run Code Online (Sandbox Code Playgroud)
它与 MediatR 一起在控制器上使用,如下所示:
await Mediator.Send(
new SearchQuery<TEntity>(
new PageInformation(pageNumber, pageSize), Request.Query));
Run Code Online (Sandbox Code Playgroud)
我已在我的应用程序中将 MediatR 注册到服务集合中,如下所示:
services.AddMediatR(Assembly.GetExecutingAssembly());
Run Code Online (Sandbox Code Playgroud)
该类SearchQuery位于正确的命名空间中,它与已创建的所有其他类位于同一位置,但是当我运行该应用程序时,我收到以下错误。
MediatR.IRequestHandler
2[AssetManagement.Core.Application.Search.Queries.SearchQuery1[AssetManagement.Core.Domain.Entities.TEntity]、AssetManagement.Core.Application.Models.PaginatedList`1[AssetManagement.Core.Domain.Entities.TEntity]]。向容器注册您的处理程序。有关示例,请参阅 GitHub 中的示例
SearchQuery<T>详细班级:
public class SearchQuery<T> : IRequest<PaginatedList<T>>
{
public SearchQuery(
PageInformation pageInformation,
IQueryCollection searchQueryCollection)
{
QueryCollection = searchQueryCollection;
PageInformation = pageInformation;
}
public IQueryCollection QueryCollection { get; }
public PageInformation PageInformation { get; }
}
public class SearchQueryHandler<T>
: IRequestHandler<SearchQuery<T>, PaginatedList<T>>
{
private readonly IFormsDbContext _formsContext;
public SearchQueryHandler(IFormsDbContext formsContext)
{
_formsContext = formsContext;
}
public …Run Code Online (Sandbox Code Playgroud) 我以前用过这个,我不能为我生活中的生活弄明白什么是错的.
@using System.Data.Entity;
@using Microsoft.AspNet.Identity;
@using Microsoft.AspNet.Identity.EntityFramework;
using(ApplicationDbContext db = new ApplicationDbContext())
{
var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = User.Identity.GetUserId();
if(user != null)
{
string role = "Admin";
if (!rm.RoleExists(role))
{
var roleResult = rm.Create(new IdentityRole(role));
if (!roleResult.Succeeded) { //error stuff
};
}
if (!um.IsInRole(user, role))
{
um.AddToRole(user, role);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我跳过创建一个角色元素(因为我已经将它添加到数据库中),但当它到达'是管理员角色的用户'位时,行um.AddToRole(用户,运行,但后来我收到一个错误,说明我有违规,因为我试图复制密钥.
我不明白为什么调试时的程序说"没有这个名字的用户具有Admin的角色",稍后当它试图添加该角色时,我会看到"嘿,那个用户已经有Admin的角色,你不能再添加它"
在我使用RoleProviders之前,我必须更改我的web.config并将信息添加到那里,但我没有这样做,因为我没有!?
它可能很简单,但我被卡住了.
为了清楚起见,我如何检查当前登录的用户是否属于具有AspNet.Identity的角色"Admin".
亲切的问候
我的ASP.NET WebApi应用程序中有以下控制器:
[Route("api/PutItem/")]
[HttpPut]
public IHttpActionResult PutItem(Guid id, Item item)
{
if (!ModelState.IsValid)
}
Run Code Online (Sandbox Code Playgroud)
我在"Items"AngularJs服务中有以下内容
var doEditItem = function(item){
var deferred = $q.defer();
console.log('item', item);
var config = {
headers: { 'Authorization': "Bearer " + $rootScope.token }
//,params: {id:item.ItemId}
}
$http.put(sitesettings.url() + "api/PutItem/", item, config)
.success(function(data){
deferred.resolve(data);
})....
Run Code Online (Sandbox Code Playgroud)
我最终收到这条消息:
没有找到与请求URI'http:// localhost:63289/api/PutItem / ' 匹配的HTTP资源.",MessageDetail:"在控制器上没有找到与请求匹配的"项目"."}
我已经对参数进行了更改以添加如下项目:
jItem = {id:item.ItemId, item:item}
Run Code Online (Sandbox Code Playgroud)
然后我尝试将jItem传递给put方法,如下所示:
$http.put(sitesettings.url() + "api/PutItem/", jItem, config)
Run Code Online (Sandbox Code Playgroud)
我的应用程序选择了Guid,但不是新的Item.如果我删除Guid并发送该项目,我的应用程序会选择新项目.
我想知道如何更改我的应用程序,以便我可以发送一个Guid,应用程序也会选择新的项目.
我使用过Angular.toJson(item,false); 但这似乎并没有改变我的ASP.NET应用程序接收信息的方式.
非常感谢
我正在使用 AutoMapper ProjectTo 将 Person 实体映射到 EmployeeDto。在我的 EmployeeDto 中,我有一个想要捕获的 AddressDto 属性。因为这个人有一个地址集合,我定义了我的映射来捕获第一个:
.ForMember(d => d.Address, s => s.MapFrom(m => m.Addresses.FirstOrDefault())
Run Code Online (Sandbox Code Playgroud)
问题是,当我将它包含在我的映射中时,我收到一个错误,指出“不支持指定类型成员“IsDeleted”...”IsDeleted 是具有 [NotMapped] 属性的 Person 的基本属性。我的 EmployeeDto 不包括 IsDeleted 属性。
当我查看 IQueryable 表达式属性时,我得到了这个:
System.Data.Entity.Core.Objects.ObjectQuery`1[MyProject.Data.Core.Entities.Person]
.MergeAs(MergeOption.AppendOnly)
.Where(
// Quoted to induce a closure:
a => a.FirstName.Contains(param.Value))
.Select(
// Quoted to induce a closure:
dto => new 0_Culture=neutral_PublicKeyToken=null>
{
IsDeleted = dto.IsDeleted,
UpdateDate = dto.UpdateDate,
CreateDate = dto.CreateDate,
Id = dto.Id,
…remaining Person entity properties removed for brevity,
Addresses = dto.Addresses,
Address = …Run Code Online (Sandbox Code Playgroud) 我一直在寻找并发现这描述了如果值为 null 则返回 bool。我使用的代码来自此片段 Client client = new Client{ FirstName = "James"};
client.GetType().GetProperties()
.Where(pi => pi.GetValue(client) is string)
.Select(pi => (string) pi.GetValue(client))
.Any(value => string.IsNullOrEmpty(value));
Run Code Online (Sandbox Code Playgroud)
但我不想返回值是否为空(bool)的情况,而是想检索所有不为空的属性值。
我尝试更改代码,但没有成功。
非常感谢
编辑
public class Client
{
public string FirstName { get; set; }
public string LastName { get; set; }
//...
}
Client client = new Client();
client.FirstName = "James";
client.LastName = "";
Run Code Online (Sandbox Code Playgroud)
使用我的“Client”类,我想迭代类中的所有属性,并且当值不为 null 或空字符串时,我将返回该值,在本例中,我只会返回一个字符串“James ”。
希望这是有道理的。
我有一个按钮,当我点击它时,我希望它用值更新UI
这是我的按钮
private async void Button_Click(object sender, RoutedEventArgs e)
{
await DoItAsync();
}
Run Code Online (Sandbox Code Playgroud)
这就是DoIt!
public async Task DoSomethingAsync()
{
await Task.Run(()=>{
for(int i = 0; i < 2000; i++)
{
//labelName.Content = i.ToString();
Console.WriteLine(i);
}
});
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我运行上面的代码,单击我的按钮,调用DoItAysnc并在控制台中显示所有数字.
如果我不发表评论
labelName.Content = i.ToString();
Run Code Online (Sandbox Code Playgroud)
我得到一个异常,事实如下:调用线程无法访问此对象,因为另一个线程拥有它.
我知道Async和await不会为你解决线程问题,但我确实认为这会有效.我想知道如何使其工作,以便为每个i值更新UI,而不会使UI无响应.
对此有任何好的资源也会有所帮助.
各种各样的问候
c# ×5
.net ×1
angularjs ×1
asp.net ×1
asp.net-core ×1
async-await ×1
automapper ×1
class ×1
linq ×1
mediatr ×1
projection ×1