gpt4 book ai didi

c# - 是否可以在 url 中的操作之前定义参数

转载 作者:太空狗 更新时间:2023-10-30 00:49:47 29 4
gpt4 key购买 nike

问题和标题一样简单。

是否有可能有一个看起来像这样的路线:{controller}/{id}/{action}

这就是我现在的代码(只是一个简单的函数)(device 是我的 Controller ):

[HttpGet]
[Route("Device/{id}/IsValid")]
public bool IsValid(int id) {
return true;
}

但是当我转到以下 URL 时,浏览器说找不到页面:localhost/device/2/IsValid

当我尝试这个 URL 时,它工作正常:localhost/device/IsValid/2

那么,是否可以使用 localhost/device/2/IsValid 而不是默认路由 localhost/device/IsValid/2?以及如何做到这一点?

欢迎询问更多信息!提前致谢!

最佳答案

您正在使用属性路由。确保启用属性路由。

Attribute Routing in ASP.NET MVC 5

对于 MVC RouteConfig.cs

public class RouteConfig {

public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapMvcAttributeRoutes();

//...Other code removed for brevity
}
}

在 Controller 中

[RoutePrefix("device")]
public class DeviceController : Controller {
//GET device/2/isvalid
[HttpGet]
[Route("{id:int}/IsValid")]
public bool IsValid(int id) {
return true;
}
}

关于c# - 是否可以在 url 中的操作之前定义参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36129925/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com