【发布时间】:2020-05-01 16:14:13
【问题描述】:
我有以下问题:我想编写一个 ASP.Net 项目,用户可以在其中注册和登录,之后还可以添加或删除内容。对于用户,我有一个 UserController,对于地图,我有一个 MapController。我想有可能在 URL localhost:port/User/Login 中写入,以便用户可以通过用户控制器登录,或者当我想添加商店时,我想写入 localhost:port/map/Add_Shop 等等。
这是我的用户控制器:
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:port/User/Login 等访问 Usercontroller 中的登录功能。
【问题讨论】:
-
这些资源中有很多解释如何为 .net 核心配置路由。您尝试了哪些方法,问题是什么?
标签: c# asp.net asp.net-mvc asp.net-core asp.net-core-webapi