English 中文(简体)
如何获取具有路由属性的查询字符串
原标题:how get query string with route attribute

我的项目是.net核心,如果我打开这个网址<code>domain.com?param1=val1&;param2=val2它执行了以下操作:

public class HomeController : Controller
{
     public IActionResult Index()
        {
            return View();
        }
}

HomeController==>;Index()如果有人请求这样的URL,我想要domain.com吗?param1=val1&;param2=val2点击此控制器RouteController=>;索引()

public class RouteController : Controller
{
     [Route(@"{*:regex(([^=]+)=([^&]+))")]
     public IActionResult Index()
        {
            return View();
        }
}

我认为我应该使用[Route(@“{*:regex(([^=]+)=([^&;]+)”)],但它不起作用!

问题回答

你想实现这样的场景吗?当用户用domain.com发送请求时,它会正常路由到Home/Index,但当用户用domain.com发送请求时?{任意查询字符串},它将路由到路由/索引

我尝试自定义IRouteConstraint,并将其添加到模板化的路由中,希望这是你想要的。

public class MyRouteConstraint : IRouteConstraint
    {
        public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (httpContext.Request.Query.Count > 0)
            {
                // Route to Route/Index if query string is present
                return false;
            }

            // Route to Home/Index if no query string is present
            return true;
        }
    } 

程序.cs

            app.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}",
                constraints: new { customConstraint = new MyRouteConstraint() }
                );

            app.MapControllerRoute(
                name: "query",
                 pattern: "{controller=route}/{action=Index}"
            );

Gif演示

[Route(“/”)]属性添加到您的RouteController,这将Route/Index指定为您网站的根目录。我还提供了用于访问查询参数的适当属性。

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

[Route("/")]
public class RouteController : Controller
{
     
    public IActionResult Index([FromQuery]string param1, [FromQuery]string param2)
    {
        return View();
    }
}

还值得您熟悉asp net core中的路由https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#设置常规路线-1





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签