C# 如何从url中删除控制器和操作,并仅在MVC中获取url
编辑 我可能没有很好地表达我的问题。实际的问题不在于路由,而在于试图导出请求的实际url 该应用程序可以部署在许多不同的机器上。比如说 等等 我不能使用Action.Link或html帮助程序的原因是,我需要在将链接返回到视图之前构建链接,因为它是以JSON(datatables.net)格式填充的,而不是在呈现视图时 谢谢C# 如何从url中删除控制器和操作,并仅在MVC中获取url,c#,model-view-controller,C#,Model View Controller,编辑 我可能没有很好地表达我的问题。实际的问题不在于路由,而在于试图导出请求的实际url 该应用程序可以部署在许多不同的机器上。比如说 等等 我不能使用Action.Link或html帮助程序的原因是,我需要在将链接返回到视图之前构建链接,因为它是以JSON(datatables.net)格式填充的,而不是在呈现视图时 谢谢 public static void RegisterRoutes(RouteCollection routes) { rou
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.js/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
我需要用一个项目列表填充一个网格,作为指向不同控制器、操作和id的链接
比如,;
当我加载网格时,url如下:http://localhost:2990/Client
这很好。网格包含如下链接
JSID = "<a href='http://" + RequestUrl + "/trainingdelivery/Get/" + referral.ReferralId + "'>Training Delivery</a>",
JQUERY中的AJAX调用
oTable = $('#referralTable').dataTable(
{
"bFilter": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bAutoWidth": false,
"sAjaxSource": "../Client/Fetch",
"aoColumns": [
//client details
{"bVisible": false, "sName": "ReferralId" },
{ "sTitle": "CRN", "sName": "CRN" },
{ "sTitle": "JSID", "sName": "JSID" },
{ "sTitle": "Name", "sName": "Name" }
]
});
sAjaxSource是用数据加载网格并返回JSON的调用。返回时需要正确格式化。是!使用该方法
阅读更多关于的信息,我认为您应该使用本文中定义的域路由。解决方案是使用Request.ApplicationPath来构建url。添加
路由。mapmvcattributteroutes()如果您有MVC5,请在RoutConfig中选择code>
步骤1:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Home",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
第二步:在你的行动上面写下路线
[Route("{Id}")]
public ActionResult NewsGroupList(string Id)
{
return View();
}
你的URL:websitename.com/Id你能发布你的Global.asax文件吗?我想看看您在那里定义了哪些Url路由。public static void registerOutes(RouteCollection路由){routes.IgnoreRoute({resource}.axd/{pathInfo}”);routes.IgnoreRoute({resource}.js/{*pathInfo});routes.IgnoreRoute({*favicon}),new{favicon=@(.*/)?favicon.ico(/)?);routes.MapRoute(“默认值”//Route name“{controller}/{action}/{id}”,//URL和参数new{controller=“Home”,action=“Index”,id=”“}//参数默认值);}添加到原始帖子以提高可读性检查以下链接将有所帮助。无法真正使用操作链接,因为它正在填充网格(datatables.net),该网格需要JSon格式,并且链接未在视图(.spark文件)中生成。整个href需要在到达视图之前构建,因为网格是使用jquery ajax调用填充的。我也会在原始信息中发布。还是谢谢你
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Home",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
[Route("{Id}")]
public ActionResult NewsGroupList(string Id)
{
return View();
}