最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to capture parent URL path of masked URL - Stack Overflow

programmeradmin0浏览0评论

I have a website which has a controller that looks like this

public async Task<IActionResult> Index(string id)
{
    ViewBag.MyText = id; 
    return View()
}

Depending on the URL, if the id parameter is present, the value of the id shows on the webpage. My view displays ViewBag.MyText. EG, I visit mysite/controller/words then I see words on my web page. Likewise, if I visit mysite/controller/difwords then I see difwords on my web page.

I bought another URL which is set as a forwarder and masks the URL. Behind the scene, the new URL actually creates an iframe with my original site as the content of the iframe.

I don't seem to be able to grab the full URL that is shown in the browser from within my MVC app.

How do I get the path from the forwarding URL?

I have a website which has a controller that looks like this

public async Task<IActionResult> Index(string id)
{
    ViewBag.MyText = id; 
    return View()
}

Depending on the URL, if the id parameter is present, the value of the id shows on the webpage. My view displays ViewBag.MyText. EG, I visit mysite/controller/words then I see words on my web page. Likewise, if I visit mysite/controller/difwords then I see difwords on my web page.

I bought another URL which is set as a forwarder and masks the URL. Behind the scene, the new URL actually creates an iframe with my original site as the content of the iframe.

I don't seem to be able to grab the full URL that is shown in the browser from within my MVC app.

How do I get the path from the forwarding URL?

Share edited Mar 7 at 5:10 Ruikai Feng 12.3k1 gold badge7 silver badges16 bronze badges asked Mar 6 at 21:08 MyDaftQuestionsMyDaftQuestions 4,73117 gold badges75 silver badges140 bronze badges 3
  • words and difwords are a ROUTE. So you can add a different route in your controller so you go to a different routine for words and difwords. – jdweng Commented Mar 6 at 22:43
  • Request.UrlReferrer... which may or may not be set by the user-agent. You could also send it along with the request as a parameter or something. What's the use case? – browsermator Commented Mar 6 at 22:58
  • It is expected that it is hard/impossible to know url of hosting page from inside IFrame/IFrame's server... You need some sort of cooperation from parent page - consider to add info how secure that communication should be (i.e. query params can be easily faked, referer is unreliable as @browsermator said, messages are client side...) – Alexei Levenkov Commented Mar 6 at 23:08
Add a comment  | 

2 Answers 2

Reset to default 0

We usually call

var path = HttpContext.Request.Path;

in controller to get the path

Depending on your individual network setup, the original host might also be availale in a header.

In that case, you might find the URL, you are looking e.g. in:

  • https://developer.mozilla./en-US/docs/Web/HTTP/Headers/Origin

  • https://developer.mozilla./en-US/docs/Web/HTTP/Headers/X-Forwarded-Host

You can access these as follows.

Request.Headers["X-Forwarded-Host"];
发布评论

评论列表(0)

  1. 暂无评论