简体   繁体   English

使用常规 ASP.NET Core 6.0 使用自定义 URL 路由

[英]Use Custom URL Routing using Conventional ASP.NET Core 6.0

I want to add constraints to routing.I am using conventional routing.我想为路由添加约束。我使用的是传统路由。 Following is my code以下是我的代码

Startup.cs启动.cs


using Lab4MVC.Routing;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
//builder.Services.AddMvc(x => x.EnableEndpointRouting = false);
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    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.UseMvc(); 
//app.UseMvcWithDefaultRoute();
//app.UseMvc(routes => {
//    Routing.LoadRoutes(routes);
//});

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
    Routing.LoadRoutes(endpoints)
    
);

app.Run();



Routing.cs路由.cs

using Microsoft.AspNetCore.Builder;

namespace Lab4MVC.Routing
{
    public static class Routing
    {
        public static void LoadRoutes(IEndpointRouteBuilder routeBuilder)
        {

            routeBuilder.MapControllerRoute(name:"Route1",pattern: "/Customer/Add/{Id}",defaults: new
            {
                contoller = "Customer",
                action = "Add"
            });

            //routeBuilder.MapControllerRoute("default", "{controller=Customer}/{action=Add}");
            //routeBuilder.MapFallbackToController("Add", "Customer");
        }
    }
}

CustomerController.cs客户控制器.cs

using Microsoft.AspNetCore.Mvc;

namespace Lab4MVC.Controllers
{
    public class CustomerController : Controller
    {
        
        public IActionResult Add(int Id)
        {
            return View("CustomerScreen");
        }


        public IActionResult Update()
        {
            return View();
        }

        public IActionResult Delete()
        {
            return View();
        }
    }
}

I am learning ASP.NET Core.我正在学习 ASP.NET Core。 Struggling to call Route with the pattern.努力用模式调用 Route。 I am able to call Controller using the default route.我可以使用默认路由调用 Controller。

When adding constraint it is not called.添加约束时,它不会被调用。 Also, please guide me on how to use the following pattern lets say Customer/New/1.另外,请指导我如何使用以下模式,比如 Customer/New/1。 Here I want to change my action name.在这里,我想更改我的动作名称。 Don't want to use Add URL.不想使用添加 URL。

Please help me.请帮我。 Thanks in advance.提前致谢。

In my opinion, the correct meaning you want to express should be that you don't want others to know what method you call in the background.在我看来,你要表达的正确意思应该是你不希望别人知道你在后台调用了什么方法。 Suppose you have a get request, and the original route is /Customer/Add? param=test,假设你有一个get请求,原来的路由是/Customer/Add? param=test, /Customer/Add? param=test, you want to display the path as /Person/Add through a custom method, similar to this requirement, right? /Customer/Add? param=test,你想通过自定义方法将路径显示为/Person/Add ,类似于这个需求,对吧?

If it is like this,you can use Rewrite method.And you can refer to this Link .如果是这样,你可以使用Rewrite方法。你可以参考这个链接

It is worth noting that: app.UseRewriter() must be used before app.UseStaticFiles() .值得注意的是: app.UseRewriter()必须在app.UseStaticFiles()之前使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM