当我运行我的asp.net核心2项目时,我收到以下错误消息:
InvalidOperationException:尝试激活"ContosoUniversity.Service.Class.StudentService"时,无法解析类型"Microsoft.EntityFrameworkCore.DbContext"的服务.
这是我的项目结构:
-- solution 'ContosoUniversity'
----- ContosoUniversity
----- ContosoUniversity.Model
----- ContosoUniversity.Service
Run Code Online (Sandbox Code Playgroud)
IEntityService(相关代码):
public interface IEntityService<T> : IService
where T : BaseEntity
{
Task<List<T>> GetAllAsync();
}
Run Code Online (Sandbox Code Playgroud)
IEntityService(相关代码):
public abstract class EntityService<T> : IEntityService<T> where T : BaseEntity
{
protected DbContext _context;
protected DbSet<T> _dbset;
public EntityService(DbContext context)
{
_context = context;
_dbset = _context.Set<T>();
}
public async virtual Task<List<T>> GetAllAsync()
{
return await _dbset.ToListAsync<T>();
}
}
Run Code Online (Sandbox Code Playgroud)
实体 :
public abstract class BaseEntity {
}
public abstract class Entity<T> : BaseEntity, …Run Code Online (Sandbox Code Playgroud) c# dependency-injection asp.net-core-mvc asp.net-core asp.net-core-2.0
我想加密给定网址的Id部分,我使用SHA-1.此算法将id转换为以下字符串:
NxVhIhrfbZNzyxqtudUZdiv4DdQA9nF1Zn7CueGUiT8 = | h1bCRiN5zxexiIhHp + qNEQ0jVh/8fMGiIkeTf30LVdU =
因此,我的最终网址将是这样的:
http://localhost:9432/Product/Edit/NxVhIhrfbZNzyxqtudUZdiv4DdQA9nF1Zn7CueGUiT8=|h1bCRiN5zxexiIhHp+qNEQ0jVh/8fMGiIkeTf30LVdU=
Run Code Online (Sandbox Code Playgroud)
此URL具有一些导致请求失败的字符.例如,url中不允许使用"+".所以我使用HttpUtility.UrlEncode()了加密的Id并得到了这个字符串:
NxVhIhrfbZNzyxqtudUZdiv4DdQA9nF1Zn7CueGUiT8%3D%7ch1bCRiN5zxexiIhHp%2bqNEQ0jVh%2f8fMGiIkeTf30LVdU%3D
现在我的网址是:
http://localhost:9432/Product/Edit/NxVhIhrfbZNzyxqtudUZdiv4DdQA9nF1Zn7CueGUiT8%3d%7ch1bCRiN5zxexiIhHp%2bqNEQ0jVh%2f8fMGiIkeTf30LVdU%3d
Run Code Online (Sandbox Code Playgroud)
但是,使用上面的url会导致以下错误:
该请求包含双转义序列,并在Web服务器上配置请求筛选以拒绝双转义序列.
我可以通过在web.config中插入以下代码来忽略它:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
现在我有两个问题:
为什么结果HttpUtility.UrlEncode()会导致任何类型的错误.正如我注意到的那样,结果不包含任何网址的非法字符?
据我所知,put <requestFiltering allowDoubleEscaping="true" />不是一个好的解决方案,因为它会在应用程序中创建一个安全漏洞,那么在这种情况下最好的解决方案是什么?
在我的解决方案中,我有两个项目.其中一个是web项目,另一个是Model项目.您可以在下面看到我的解决方案结构:
现在,当我运行应用程序时,我收到以下错误:
InvalidOperationException:无法找到包'ContosoUniversity.Model'的编译库位置
这里是完整的堆栈跟踪:
An unhandled exception occurred while processing the request.
InvalidOperationException: Cannot find compilation library location for package 'ContosoUniversity.Model'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List<string> assemblies)
InvalidOperationException: Cannot find compilation library location for package 'ContosoUniversity.Model'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List<string> assemblies)
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart+<>c.<GetReferencePaths>b__8_0(CompilationLibrary library)
System.Linq.Enumerable+SelectManySingleSelectorIterator.MoveNext()
Microsoft.AspNetCore.Mvc.Razor.Compilation.MetadataReferenceFeatureProvider.PopulateFeature(IEnumerable<ApplicationPart> parts, MetadataReferenceFeature feature)
Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateFeature<TFeature>(TFeature feature)
Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorReferenceManager.GetCompilationReferences()
System.Threading.LazyInitializer.EnsureInitializedCore<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorReferenceManager.get_CompilationReferences()
Microsoft.AspNetCore.Mvc.Razor.Internal.LazyMetadataReferenceFeature.get_References()
Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.ExecuteCore(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document)
Microsoft.AspNetCore.Razor.Language.RazorTemplateEngine.GenerateCode(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CompileAndEmit(string relativePath)
Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CreateCacheEntry(string normalizedPath)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() …Run Code Online (Sandbox Code Playgroud) 当我想向我的项目添加迁移时,我收到以下错误:
dotnet:找不到匹配命令"dotnet-ef"的可执行文件
为了解决这个问题,我添加了以下包,但我仍然得到相同的错误.
Microsoft.EntityFrameworkCore.Design(2.0.0)
Microsoft.EntityFrameworkCore.Tools.DotNet(2.0.0)
Run Code Online (Sandbox Code Playgroud)
我找到了一些解决方案,但这些是基于.net-core-1和.net-core-2我们没有project.json文件.
更新:
这是我的.csproj文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud) 我想在JS插件中使用带有razor语法的C#字符串数组.
C#代码:( 在cshtml中)
@{
string[] extentions = new string[] { "jpg", "png", "gif", "jpeg", "pdf" };
}
Run Code Online (Sandbox Code Playgroud)
JS代码:
$('#file').filer({
limit: 2,
maxSize: 4000,
extensions: ["jpg", "png", "gif", "jpeg", "pdf"],
...
})
Run Code Online (Sandbox Code Playgroud)
带有C#string []的JS代码:
$('#file').filer({
limit: 2,
maxSize: 4000,
extensions: '@extentions',
...
})
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我得到System.String[],如果我使用JsonConvert.SerializeObject(extentions)我得到这样的东西:
["jpg","png","gif","jpeg","pdf"]
Run Code Online (Sandbox Code Playgroud)
以我想要的格式将c#字符串数组转换为Js数组的最佳方法是什么?
我希望按周分组我的数据,如下所示:
var result = stats.GroupBy(i => SqlFunctions.DatePart("week", i.date))
.Select(g => new ReportModel
{
clicks = g.Select(x => x.clicks).Sum(),
impressions = g.Select(x => x.impressions).Sum(),
...
});
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
只能从LINQ到实体调用此函数.
有什么问题,我该如何解决?
假设我们有一个这样的字符串:2002.我想删除最后一个字符并舍入结果的最后一个字符.这意味着正确的输出是:201.
注意:如果最后一个字符是0我们不需要向上舍入;
更多例子:
100--- to:---10
200--- to:---20
101--- to:---11
222--- to:---23
30006--- to:---3001
1299--- to:---130
4089--- to:---409
5099--- to:---510
实际上它是Rial到Toman的货币转换的解决方案