gpt4 book ai didi

c# - 如何从 url 中删除 Controller 和操作,只获取 MVC 中的 url

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:47 24 4
gpt4 key购买 nike

编辑我可能没有很好地表达我的问题。实际问题不在于路由,而在于尝试导出请求的实际 url。

该应用程序可以部署在许多单独的机器上。例如

http://localhost:2990/Client/List

http://avapd024/llne.dev/Client/List

http://machine123:81/llne.prod/Client/List

等等等等

我不能使用 Action.Link 或 html 助手的原因是我需要在将它返回到 View 之前构建链接,因为它填充为采用 JSON (datatables.net) 而不是的网格以及 View 的时间正在渲染

谢谢

我需要用一个项目列表填充一个网格,作为指向不同 Controller 、操作和 ID 的链接。

例如;当我加载网格时,url 如下:http://localhost:2990/Client 这很好。网格包含如下所示的链接

JSID = "<a href='http://" + RequestUrl + "/trainingdelivery/Get/" + referral.ReferralId + "'>Training Delivery</a>",

Controller 是“trainingdelivery”,操作是“Get”,id = referralid 会生成类似 http://localhost:2990/trainingdelivery/Get/12345 的 url。这在我的本地机器上很好,但是当我们部署它时,它会转到具有不同 url 的服务器,如 http://avapd024/llne.dev:81/ 正如你可以看到 'llne.dev: 81' 是 IIS 网络,我需要获取它所在位置的 url 以识别其位置并转到正确的 Controller 和操作。

我尝试使用请求对象,我可能会在那里乱搞以获得它,但我想知道是否有更好的解决方案?

谢谢

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
);
}

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 的调用。返回时需要正确格式化。

最佳答案

是的!使用 HtmlHelper.ActionLink()方法。

JSID = <%= Html.ActionLink("Get", 
new { controller = "trainingdelivery", id = referral.ReferralId}) %>

假设您已经在 Global.asax 文件中设置了如下所示的路由:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MyApp.MVC
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"TrainingDelivery",
"trainingdelivery/{action}/{id}",
new { controller = "TrainingDelivery", action = "Get", id = 0 }
);
}

protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
}
}

阅读更多关于 ASP.Net Routing 的信息

关于c# - 如何从 url 中删除 Controller 和操作,只获取 MVC 中的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2174998/

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