C# ASP.NET Core 3.0中的路由
我有以下问题:我想写一个ASP.Net项目,用户可以注册和登录,然后添加或删除内容。对于用户,我有一个UserController,对于地图,我有一个MapController。我想有可能写在URL localhost:port/User/Login中,这样用户就可以通过用户控制器登录,或者当我想添加店铺时,我想写localhost:port/map/add\u Shop等等 这是我的用户控制器:C# ASP.NET Core 3.0中的路由,c#,asp.net,asp.net-mvc,asp.net-core,asp.net-core-webapi,C#,Asp.net,Asp.net Mvc,Asp.net Core,Asp.net Core Webapi,我有以下问题:我想写一个ASP.Net项目,用户可以注册和登录,然后添加或删除内容。对于用户,我有一个UserController,对于地图,我有一个MapController。我想有可能写在URL localhost:port/User/Login中,这样用户就可以通过用户控制器登录,或者当我想添加店铺时,我想写localhost:port/map/add\u Shop等等 这是我的用户控制器: using System; using System.Collections.Generic; u
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Soft_Arch_WebAPI.Models;
namespace Soft_Arch_WebAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
[HttpPost]
public String Register()
{
return "Hier werden User registriert";
//Get Values from Form and Send to Service
}
[HttpPost]
public String Login()
{
return "Hier werden User eingeloggt";
//Get Values from Form and Send to Service
}
[HttpPut]
public String ResetPWD(int ID, String Password)
{
return "Hier können User Ihr Passwort ändern";
//Get Values from Form and Send to Service
}
}
}
这是我的Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Soft_Arch_WebAPI
{
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.AddControllersWithViews();
}
// 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();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "user",
pattern: "User/Register/");
endpoints.MapControllerRoute(
name: "user",
pattern: "User/Login/");
endpoints.MapControllerRoute(
name: "user",
pattern: "User/ResetPWD/");
endpoints.MapControllerRoute(
name: "map",
pattern: "{controller=Map}/Add_Shop/");
endpoints.MapControllerRoute(
name: "map",
pattern: "{controller=Map}/Edit_Shop/");
endpoints.MapControllerRoute(
name: "favourite",
pattern: "{controller=Favourite}/Add_Favourite/");
endpoints.MapControllerRoute(
name: "favourite",
pattern: "{controller=Favourite}/Delete_Favourite/");
endpoints.MapControllerRoute(
name: "poi",
pattern: "{controller=Poi}/Add_Poi/");
endpoints.MapControllerRoute(
name: "poi",
pattern: "{controller=Poi}/Delete_Poi/");
});
}
}
}
如何设置前面提到的路由,例如通过localhost访问Usercontroller中的登录功能:port/User/login等等。@Simon似乎在为User Controller使用属性路由。我们可以在控制器本身中使用操作方法登录的属性,如下所示:
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Soft_Arch_WebAPI.Models;
namespace Soft_Arch_WebAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
[HttpPost("Register")]
public String Register()
{
return "Hier werden User registriert";
//Get Values from Form and Send to Service
}
[HttpPost("login")]
public String Login()
{
return "Hier werden User eingeloggt";
//Get Values from Form and Send to Service
}
[HttpPut]
public String ResetPWD(int ID, String Password)
{
return "Hier können User Ihr Passwort ändern";
//Get Values from Form and Send to Service
}
}
}
因为我们正在为用户控制器使用属性路由,所以可以从Startup.cs中删除用户控制器的条目。
我希望这会有所帮助。正如Hemant Kumar所说,您需要手动配置,而不是在asp.net核心web api中配置
UseEndpoints
[ApiController]
属性使属性路由成为一项要求
通过由定义的常规路线无法访问操作
UseEndpoints
,UseMvc
,或在Startup.Configure中的UseMvcWithDefaultRoute
有大量的参考资料解释了如何为.net core配置路由。你尝试过什么?问题是什么?