小编Kaa*_*Mai的帖子

.NET 6 MVC 启用 razor 运行时编译会破坏 program.cs

我按照https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-6.0&tabs=visual-studio上的简单步骤启用 cshtml 视图的运行时编译。

但是,一旦我将“.AddRazorRuntimeCompilation()”添加到构建器中:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages()
    .AddRazorRuntimeCompilation();
Run Code Online (Sandbox Code Playgroud)

这会导致项目运行时出错:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
Run Code Online (Sandbox Code Playgroud)
System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null. (Parameter 'source')
  Source=System.Linq
  StackTrace:
   at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.OfType[TResult](IEnumerable source)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionDescriptorProvider..ctor(IEnumerable`1 pageRouteModelProviders, IOptions`1 mvcOptionsAccessor, IOptions`1 pagesOptionsAccessor)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc

5
推荐指数
1
解决办法
6385
查看次数

LINQ在字符串上放置不需要的尾随空格

我对LINQ和MS SQL Server有一个非常烦人的问题.

我目前正在WPF C#中制作一些软件,并使用LINQ从数据库中的数据生成类.

但是,当我从程序中更新DB中的nvarchar或varchar字段时,LINQ会在字符串中添加尾随空格!

我在这样定义的表中有一个字段:

ProductName = NVARCHAR(10)
Run Code Online (Sandbox Code Playgroud)

所以,如果我这样做:

Product.Name = "Bike";
Product.Name = Product.Name.Trim();
repository.Add(Product);   // Runs an .InsertOnSubmit
repository.Save();         // Runs a .SubmitChanges
Run Code Online (Sandbox Code Playgroud)

然后DB中的结果数据是"Bike [SPACE] [SPACE] [SPACE] [SPACE] [SPACE] [SPACE]",其中[SPACE]只是一个空格(此处不能在此文本中写​​入多个空格).

为什么会这样做,我如何让它停止添加那些烦人的尾随空格?

提前致谢

linq sql-server spaces

3
推荐指数
1
解决办法
1979
查看次数

标签 统计

asp.net-core-mvc ×1

c# ×1

linq ×1

spaces ×1

sql-server ×1