C# 匹配自定义路由中的任何操作
我需要创建一个自定义路线图,允许我匹配特定url映射中的任何操作。 示例:C# 匹配自定义路由中的任何操作,c#,asp.net-mvc,url,routing,asp.net-mvc-routing,C#,Asp.net Mvc,Url,Routing,Asp.net Mvc Routing,我需要创建一个自定义路线图,允许我匹配特定url映射中的任何操作。 示例:www.site.com/patient/records/treatments/23其中treatments可以是pacient controller中的任何操作 以下是我尝试过但不起作用的内容: routes.MapRoute("records_ho", "{controller}/records/{action}/{recordid}", new { controller = "patien
www.site.com/patient/records/treatments/23
其中treatments
可以是pacient controller中的任何操作
以下是我尝试过但不起作用的内容:
routes.MapRoute("records_ho", "{controller}/records/{action}/{recordid}", new {
controller = "patient", recordid = UrlParameter.Optional
});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "User", id = UrlParameter.Optional }
);
您可能已经注意到,我没有在“records_ho”中指定action属性,这是因为我希望避免在MapRoute中指定controllerPacient
中定义的15个操作
我怎样才能做到这一点
更新:
这就是行动
[HttpGet]
public ActionResult Treatments(string recordid)
{
// some code here...
return View(model);
}
理论上,一切都应按预期进行,见下文 路由代码:
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication6
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/records/{action}/{recordid}",
defaults: new { controller = "patient", recordid = UrlParameter.Optional }
);
}
}
}
using System.Web.Mvc;
namespace WebApplication6.Controllers
{
public class PatientController : Controller
{
[HttpGet]
public ActionResult Treatments(string recordid)
{
return View();
}
}
}
Request URL:http://localhost:29930/patient/records/treatments/23
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:29930
Referrer Policy:no-referrer-when-downgrade
Cache-Control:private
Content-Encoding:gzip
Content-Length:1538
Content-Type:text/html; charset=utf-8
Date:Fri, 21 Apr 2017 20:42:02 GMT
Server:Microsoft-IIS/10.0
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.2
X-Powered-By:ASP.NET
控制器代码:
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication6
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/records/{action}/{recordid}",
defaults: new { controller = "patient", recordid = UrlParameter.Optional }
);
}
}
}
using System.Web.Mvc;
namespace WebApplication6.Controllers
{
public class PatientController : Controller
{
[HttpGet]
public ActionResult Treatments(string recordid)
{
return View();
}
}
}
Request URL:http://localhost:29930/patient/records/treatments/23
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:29930
Referrer Policy:no-referrer-when-downgrade
Cache-Control:private
Content-Encoding:gzip
Content-Length:1538
Content-Type:text/html; charset=utf-8
Date:Fri, 21 Apr 2017 20:42:02 GMT
Server:Microsoft-IIS/10.0
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.2
X-Powered-By:ASP.NET
和请求/响应:
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication6
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/records/{action}/{recordid}",
defaults: new { controller = "patient", recordid = UrlParameter.Optional }
);
}
}
}
using System.Web.Mvc;
namespace WebApplication6.Controllers
{
public class PatientController : Controller
{
[HttpGet]
public ActionResult Treatments(string recordid)
{
return View();
}
}
}
Request URL:http://localhost:29930/patient/records/treatments/23
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:29930
Referrer Policy:no-referrer-when-downgrade
Cache-Control:private
Content-Encoding:gzip
Content-Length:1538
Content-Type:text/html; charset=utf-8
Date:Fri, 21 Apr 2017 20:42:02 GMT
Server:Microsoft-IIS/10.0
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:5.2
X-Powered-By:ASP.NET
@EugeneKomisarenko我对MapHttpRoute的api路由知之甚少,但是的,似乎是这样。你测试过自己的路由吗?我刚刚尝试过并且没有问题,例如,能够呈现
/patient/records/index/100
和/patient/records/contact/100
,没有问题。您遇到了什么错误?即使没有指定操作属性?请使用异常详细信息更新您的问题,以查看发生了什么。您遇到了什么错误?什么不起作用?我在用?测试通过参数?。这就是为什么它没有制造任何东西。谢谢没问题,和别人分享问题总是有帮助的!