When / Why should I use the C # method "WebPageExecutingBase.Href ()"?

I tried to find Google and Stack Exchange to answer this question, and although it did provide some documentation on the Href() method (the documentation I saw earlier). It seems that the questions on this method are either very obscure or nonexistent, and I still wonder why / when to really use it.

MSDN Documentation: http://msdn.microsoft.com/en-us/library/system.web.webpages.webpageexecutingbase.href(v=vs.111).aspx

In this documentation, I can say that it builds an absolute URL from a relative path.

My questions:

  • Why do I need it?
  • Should the relative path work perfectly outside the testing environment and even on the web server?
  • Do I have to change EVERY one of the relative paths on my site to include the Href() method? (Example: Change Context.RedirectLocal("~/") to Context.RedirectLocal(Href("~/")) )
  • What are the best practices for using this method based on your ASP.NET web page environment?

I apologize for being so confused that it seems so simple, but I would not want my site to live just to find out that it is broken or has security holes (first impressions can be a killer).

+4
source share
1 answer

You explicitly use the method if you are working with web pages 1, and you want your virtual paths to always display correctly in an absolute URL. From web pages 2, the Href method is called by the framework if the parser encounters a tilde (~) in the URL in your cshtml file, for example.

 <script type="text/javascript" src="~/Scripts/jquery.js"></script> 

When can a path be resolved incorrectly without using the HREF method explicitly or tilde? This may not work if the structure of the root root site changes, if you, for example, change the hosting. It may also not work if your internal folder structure changes, or if you move files. If this is unlikely, you probably don't need to worry about using the method. I did not seek to use it until the Khref method was replaced by a tilde. Now I always use it based on the fact that it is much easier to use, and I would prefer to add another keystroke to each URL than regret at some point in the future.

You can learn more about Href about halfway down this page: http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the- razor syntax

+3
source

Source: https://habr.com/ru/post/1499648/


All Articles