在C#ASP.NET Core 3.1应用程序中,我如何路由到控制器然后查询字符串?

发布于 2025-01-26 05:33:34 字数 549 浏览 4 评论 0原文

我有一个视图设置为:

”在此处输入图像说明”

然后我有我的业务控制器:

[Route("{business}/{url}")]
public IActionResult business(string url)
{
    return View();
}

我的目的是能够像

https://website.com/business/business-name-123

business> business-name-123然后接收为参数。

在您通过?query =网站名< / code>之前,我已经创建了类似的东西?

I have a view setup as:

enter image description here

I then have my Business controller:

[Route("{business}/{url}")]
public IActionResult business(string url)
{
    return View();
}

My aim is to be able to pass the url string 'neatly' like so

https://website.com/business/business-name-123

The business-name-123 is then received as the parameter.

I have created something similar to this before where you pass ?query=website-name but I don't want this, can someone explain what I need to do to get this working using just the / routing aspect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2025-02-02 05:33:34

在路由模板中删除{business}周围的括号。业务是一个恒定的字符串,而不是路由参数:

[Route("business/{url}")]

Remove the braces around {business} in your route template. Business is a constant string, not a route parameter:

[Route("business/{url}")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文