我正在尝试使用this tutorial将OAuth 2.0添加到我的.NET Core 3.0 Web Api。以下是WebApiConfig类的内容。
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute( // HERE IS THE ERROR
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// WebAPI when dealing with JSON & JavaScript!
// Setup json serialization to serialize classes to camel (std. Json format)
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
// Adding JSON type web api formatting.
config.Formatters.Clear();
config.Formatters.Add(formatter);
}
我猜这与依赖关系有关,所以这里是csproj:
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNet.WebApi.Owin" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNet.WebApi.WebHost" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.Owin.Cors" Version="4.1.0" />
<PackageReference Include="Microsoft.Owin.Security.OAuth" Version="4.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
我看到了一个类似的问题,答案是建议使用app.UseMvc映射路线,但他使用的是.NET版本(2.0)。我也尝试使用app.UseMvc,但是它什么也不做,因为我使用EndpointRouting。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<AppUser, AppRole>(options =>
{
options.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<AppDbContext>();
services.AddControllers();
//+adding database and service/repository
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
所以我不太确定该怎么做。我尝试关闭Visual Studio并删除项目中的每个obj,bin和vs文件夹(听起来有些奇怪,有人建议这样做),但没有成功。
有什么建议吗?
答案 0 :(得分:1)
这也是我遇到的一个棘手的问题,但是当我搜索此错误消息的解决方案时,似乎没人能找到一个优雅的解决方案……所以我找到了自己。
这里有几个问题在起作用:
如错误消息所指出,已定义方法MapHttpRoute但找不到
因此,这解决了实现MapHttpRoute方法的问题...但这并不一定意味着您可以轻松进行服务调用!
这是因为此处要实现的HttpDelete服务具有调用参数,必须通过[Route]和[HttpDelete]属性定义这些参数,这将导致我们进入步骤2。
我们需要通过[Route]和[HttpDelete]属性明确声明调用参数
routeTemplate =“ api / {controller} / {userID} / {userName}”;
[Route(“ [controller] / {userID} / {userName}”)]
[HttpDelete(“ {userID} / {userName}”)]
公共布尔Delete(字符串userID,字符串userName)=> this.DeleteUser(userID,userName);
[Route(“ [controller]”)]
公共布尔DeleteUser(字符串userIDParam,字符串userNameParam)
{
int? userID = 0; 字符串userName =“”;
如果(userIDParam!= null) userID = Int16.Parse(userIDParam);
如果(userNameParam!= null) userName = userNameParam;
返回DeleteSpecifiedUser(userID,userName);
}