问题描述
我的DxDatagrid
页面上有以下dotnet core webapp index.cshtml
块:
@(Html.DevExtreme().DataGrid<usermodel>()
.ID("grid-container")
.ShowBorders(true)
.DataSource(d => d.Mvc().Controller("UserSearch").LoadAction("Get").Key("UserId"))
.Selection(s => s
.Mode(SelectionMode.Multiple)
.SelectAllMode(SelectAllMode.Page)
)
使用此代码并使用dotnet core 2.2
,数据源将调用:
http://localhost:5000/api/UserSearch/Get?skip=0&take=10&requiretotalCount=true&_=1600859370033
已经更新到dotnet core 3.1并更新了csproj
和_Layout.cshtml
文件中的DevExpress引用,路由现在尝试调用:
http://localhost:5000/?skip=0&take=10&requiretotalCount=true&_=1600859693687
startup.cs
是这个:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using AccessUsers.Middleware;
using AccessUsers.Models;
using Microsoft.AspNetCore.HttpOverrides;
namespace WebAppTest
{
public class Startup
{
private readonly IConfiguration _config;
private readonly AppSettings _appSettings;
public Startup(IConfiguration config)
{
_config = config;
_appSettings = _config.Get<AppSettings>();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
// options.MinimumSameSitePolicy = SameSiteMode.None;
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
options.OnAppendCookie = cookieContext => cookieContext.CookieOptions.SameSite = SameSiteMode.Unspecified;
options.OnDeleteCookie = cookieContext => cookieContext.CookieOptions.SameSite = SameSiteMode.Unspecified;
});
services.Configure<AppSettings>(_config);
services.AddSingleton<APIService>();
services.AddSingleton<UserService>();
services.AddSingleton<ShipToService>();
services.AddApplicationInsightsTelemetry();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddSession();
services.AddMemoryCache();
services.AddRazorPages().AddNewtonsoftJson(options => {
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
}).AddXmlSerializerFormatters();
services.USEOpenIDConnectMiddleware(new OpenIDConnectMiddlewareOptions
{
BaseUrl = _appSettings.API.BaseUrl,AppName = _appSettings.AppName,ClientId = _appSettings.API.ClientId,ClientSecret = _appSettings.API.ClientSecret,Secure = !_appSettings.Local
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,IHostingEnvironment env)
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedProto
});
if (_appSettings.Local)
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
app.UseGlobalLoginMiddleware();
app.UseHttpsRedirection();
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints => {
endpoints.MapRazorPages();
});
CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
string location = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
var supportedCultures = allCultures.Where(c => Directory.Exists(Path.Combine(location,c.Name)) && c.LCID != 127).ToList();
app.UseRequestLocalization(new RequestLocalizationoptions
{
DefaultRequestCulture = new RequestCulture("en-US"),SupportedCultures = supportedCultures,SupportedUICultures = supportedCultures
});
}
}
}
csproj
包含以下内容:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<packagereference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.6" />
<packagereference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.7" />
<packagereference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.7.1" />
<packagereference Include="DevExtreme.AspNet.Data" Version="2.7.1" />
<packagereference Include="DevExtreme.AspNet.Core" Version="20.1.7" />
<packagereference Include="Microsoft.IdentityModel.Tokens" Version="6.7.1" />
<packagereference Include="System.IdentityModel.Tokens.Jwt" Version="6.7.1" />
<packagereference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.14.0" />
<packagereference Include="Amazon.Lambda.AspNetCoreServer" Version="5.0.0" />
</ItemGroup>
</Project>
controller.cs
包含以下内容:
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using AccessUsers.Models;
using DevExtreme.AspNet.Data;
using DevExtreme.AspNet.Mvc;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace WebAppTest.Pages
{
[Route("api/[controller]/[action]")]
public class UserSearchController : Controller
{
private readonly UserService _userService;
public UserSearchController(UserService userService)
{
_userService = userService;
}
[HttpGet]
public object Get(DataSourceLoadOptions loadOptions)
{
var result = DataSourceLoader.Load(GetProfiles(user:new usermodel(),useDummyData: true),loadOptions);
return result;
}
_Layout.cshtml
包含以下内容:
<script src="https://cdn3.devexpress.com/jslib/20.1.7/js/dx.all.js" integrity="sha384-LAn+t9UxSqkm8biNuoUbJcohKoYmbiFRfVLERIJ4I3RyEpAIBizEcIztuXPG9Cqg sha512-OAjfsw+eXv345AD9H6kDJLChXetpJD6ChGgDvjVIEumiHYulOLXIO/Do5gxljW2GUgpObic42JyS8a0wZqb1Fw==" crossorigin="anonymous"></script>
<script src="https://cdn3.devexpress.com/jslib/20.1.7/js/dx.aspnet.mvc.js" integrity="sha384-5rtF4jUX5Hez5YwkW7PHC/0XplJQS26qVUCfec8fBX0IkoR1y35EXHkZDbgeMh3x sha512-0eJebJTnN45FCtUOrVqxk5p73OMWsx94vLQpnlRtDp/CKbssiUR0j0os+0y01fvzDtdtnEKSeau32g30fgtrYQ==" crossorigin="anonymous"></script>
如此处指定: https://js.devexpress.com/Documentation/Guide/Common/Distribution_Channels/CDN/
我确定是对dotnet core 3.1
的更改导致路由中断,因为应用程序的功能没有更改,但是我看不到是什么导致了中断。
解决方法
Startup.ConfigureServices
不添加对控制器的支持,仅添加具有:
services.AddRazorPages().AddNewtonsoftJson(options => {
...
}).AddXmlSerializerFormatters();
从Remarks in the method's documentation
此方法为页面的常用功能配置MVC服务。
要为API的控制器添加服务,请调用AddControllers(IServiceCollection)。
该控制器现在从未注册过,因此尝试生成操作URL的代码
.DataSource(d => d.Mvc().Controller("UserSearch").LoadAction("Get")
找不到任何内容并返回空字符串。
要解决此问题,请添加控制器支持:
services.AddControllers().AddNewtonsoftJson(options => {
...
}).AddXmlSerializerFormatters();
services.AddRazorPages();
也应在Configure
的端点路由代码中使用MapControllers添加控制器:
app.UseEndpoints(endpoints => {
endpoints.MapRazorPages();
endpoints.MapControllers();
});