200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 获取ASP.NET MVC中的完整操作URL [重复]

获取ASP.NET MVC中的完整操作URL [重复]

时间:2023-03-19 18:43:08

相关推荐

获取ASP.NET MVC中的完整操作URL [重复]

本文翻译自:Getting full URL of action in MVC [duplicate]

This question already has an answer here:这个问题在这里已有答案:

How do I find the absolute url of an action in MVC?如何在 MVC中找到动作的绝对URL?9 answers9个答案

Is there a built-in way of getting the full URL of an action?是否有内置的方法来获取操作的完整URL?

I am looking for something likeGetFullUrl("Action", "Controller")that would return something like/Controller/Action.我正在寻找像GetFullUrl("Action", "Controller")东西,它会返回像/Controller/Action

The reason I am looking for this is to avoid hardcoding URLs in automated emails that are generated so that the URLs can always be generated relative to the current location of the site.我正在寻找这个的原因是为了避免在生成的自动电子邮件中硬编码URL,以便始终可以相对于站点的当前位置生成URL。

#1楼

参考:/question/8Pgd/获取ASP-NET-MVC中的完整操作URL-重复

#2楼

There is an overload of Url.Action that takes your desired protocol (eg http, https) as an argument - if you specify this, you get a fully qualified URL.Url.Action过载会将您所需的协议(例如http,https)作为参数 - 如果您指定此参数,则会获得完全限定的URL。

Here's an example that uses the protocol of the current request in an action method:这是一个在动作方法中使用当前请求的协议的示例:

var fullUrl = this.Url.Action("Edit", "Posts", new { id = 5 }, this.Request.Url.Scheme);

HtmlHelper (@Html) also has an overload of the ActionLink method that you can use in razor to create an anchor element, but it also requires the hostName and fragment parameters.HtmlHelper(@Html)也有一个ActionLink方法的重载,您可以在razor中使用它来创建一个锚元素,但它还需要hostName和fragment参数。So I'd just opt to use @Url.Action again:所以我只是选择再次使用@ Url.Action:

<span>Copy<a href='@Url.Action("About", "Home", null, Request.Url.Scheme)'>this link</a> and post it anywhere on the internet!</span>

#3楼

This question is specific to ASP .NET however I am sure some of you will benefit of system agnostic javascript which is beneficial in many situations.这个问题是ASP .NET特有的,但我相信你们中的一些人会受益于系统无关的javascript,这在很多情况下是有益的。

UPDATE:The way to get url formed outside of the page itself is well described in answers above.更新:在上面的答案中很好地描述了在页面外部形成url的方法。

Or you could do a oneliner like following:或者你可以做一个如下的oneliner:

new UrlHelper(actionExecutingContext.RequestContext).Action("SessionTimeout", "Home", new {area = string.Empty}, actionExecutingContext.Request.Url!= null? actionExecutingContext.Request.Url.Scheme : "http");

from filter or:来自过滤器或:

new UrlHelper(this.Request.RequestContext).Action("Details", "Journey", new { area = productType }, this.Request.Url!= null? this.Request.Url.Scheme : "http");

However quite often one needs to get the url of current page, for those cases usingHtml.Actionand putting he name and controller of page you are in to me feels awkward.然而,通常需要获取当前页面的URL,因为对于那些使用Html.Action并且将他的名字和控制器放在我身上感觉很尴尬。My preference in such cases is to use JavaScript instead.在这种情况下,我的偏好是使用JavaScript代替。This is especially good in systems that are half re-written MVT half web-forms half vb-script half God knows what - and to get URL of current page one needs to use different method every time.这在半重写的系统中特别好。一半的网络形式一半vb-脚本一半上帝知道什么 - 并且要获得当前页面的URL每次需要使用不同的方法。

One way is to use JavaScript to get URL iswindow.location.hrefanother -document.URL一种方法是使用JavaScript获取URL,另一种是window.location.href-document.URL

#4楼

This may be just me being really, really picky, but I like to only define constants once.这可能只是我真的,​​非常挑剔,但我喜欢只定义一次常量。If you use any of the approaches defined above, your action constant will be defines multiple times.如果您使用上面定义的任何方法,您的动作常量将定义多次。

To avoid this, you can do the following:为避免这种情况,您可以执行以下操作:

public class Url{public string LocalUrl { get; }public Url(string localUrl){LocalUrl = localUrl;}public override string ToString(){return LocalUrl;}}public abstract class Controller{public Url RootAction => new Url(GetUrl());protected abstract string Root { get; }public Url BuildAction(string actionName){var localUrl = GetUrl() + "/" + actionName;return new Url(localUrl);}private string GetUrl(){if (Root == ""){return "";}return "/" + Root;}public override string ToString(){return GetUrl();}}

Then create your controllers, say for example the DataController:然后创建你的控制器,例如DataController:

public static readonly DataController Data = new DataController();public class DataController : Controller{public const string DogAction = "dog";public const string CatAction = "cat";public const string TurtleAction = "turtle";protected override string Root => "data";public Url Dog => BuildAction(DogAction);public Url Cat => BuildAction(CatAction);public Url Turtle => BuildAction(TurtleAction);}

Then just use it like:然后就像使用它:

// GET: Data/Cat[ActionName(ControllerRoutes.DataController.CatAction)]public ActionResult Etisys(){return View();}

And from your .cshtml (or any code)从您的.cshtml(或任何代码)

<ul><li><a href="@ControllerRoutes.Data.Dog">Dog</a></li><li><a href="@ControllerRoutes.Data.Cat">Cat</a></li></ul>

This is definitely a lot more work, but I rest easy knowing compile time validation is on my side.这肯定是更多的工作,但我很容易知道编译时验证在我身边。

#5楼

这就是你需要做的。

@Url.Action(action,controller, null, Request.Url.Scheme)

#6楼

I was having an issue with this, my server was running behind a load balancer.我遇到了这个问题,我的服务器在负载均衡器后面运行。The load balancer was terminating the SSL/TLS connection.负载均衡器正在终止SSL / TLS连接。It then passed the request to the web servers using http.然后它使用http将请求传递给Web服务器。

Using the Url.Action() method with Request.Url.Schema, it kept creating a http url, in my case to create a link in an automated email (which my PenTester didn't like).在Request.Url.Schema中使用Url.Action()方法,它一直在创建一个http url,在我的例子中,在自动电子邮件中创建一个链接(我的PenTester不喜欢)。

I may have cheated a little, but it is exactly what I needed to force a https url:我可能有一点作弊,但这正是我需要强制https网址:

<a href="@Url.Action("Action", "Controller", new { id = Model.Id }, "https")">Click Here</a>

I actually use a web.config AppSetting so I can use http when debugging locally, but all test and prod environments use transformation to set the https value.我实际上使用web.config AppSetting所以我可以在本地调试时使用http,但所有测试和prod环境都使用转换来设置https值。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。