How to get current request url in Sharepoint 2013 in C#? - c#

How to get current request url
I type in browser:
http://srv-1/sites/1001/Account Documents/Order
but in Page_Load in my controls I get
HttpContext.Current.Request.Url = http://srv-1/_layouts/15/start.aspx
and
Page.Request.RawUrl = Page.Request.RawUrl
I need to retrieve information: /Account Documents/Order

This happens because of Minimal Download Strategy feature. Your url is rewritten by SharePoint.
Easiest solution is to disable this feature, but you can also try to get url via SPUtility.OriginalServerRelativeRequestUrl property or refactor your code not use url, but current library or something else.

SharePoint does it’s own URL rewrites, if you would try that for example for the page
The solution is to use special property on [SPUtility class – SPUtility.OriginalServerRelativeRequestUrl][1]
http://blog.voyta.net/2012/07/09/how-to-get-original-request-url-in-sharepoint/
This property returns the original URL before it was rewritten, which is useful if you need to get the subweb from which an application page was loaded.
As this URL is server-relative, to get full url, you can use:
SPUtility.GetFullUrl(SPContext.Current.Site,
SPUtility.OriginalServerRelativeRequestUrl);
This can be useful if you need to redirect for example to the same URL with some additional parameters.
Thanks

Related

Updating the displayed URL in the browser using .NET Core

So, I have a certain webpage (view) that I have created. I have a requirement where I need to update the displayed URL in the browser's to show a different path to this page and update the querystring.
Update: I don't want to actually redirect the page, this is merely a cosmetic update. To make the URL appear differently that what it was. It's a requirement our customer support team wanted. :p
Ex.
https://www.myserver.com/error/
I need to update the path in the URL depending on the type of error, like so:
https://www.myserver.com/#/order-completed?var=someguid
My error page handles various situations you see.
I know this is easily done in JS, but I want to be able to do this from my error page Controller.
Could someone lend a hand? I'd super appreciate it!
I think "update the path" means you simply have to redirect the browser to that url. If you are using ASP.NET MVC, you can use the Redirect controller method like this:
return Redirect("https://www.myserver.com/#/order-completed?var=someguid");
So, I went the way of JS afterall. I call it from window.onload in the View.
var fromController = '#ViewData["NewURL"]';
histoy.pushState(null, '', fromController);
In the Controller, in the Index() action
ViewData["NewURL"] = #"/myURL/myview?user=2342434";
return View();

How to get the previous url in C#?

How to get the previous url in a MasterPage in C# ?
I'm trying to find the page which is redirected from.
Thanks in advance.
You can get information of the previous url with the UrlReferrer property. This works in MVC and Web forms.
Request.UrlReferrer.AbsoluteUri
Note that in the first page the property Request.UrlReferrer will be null. Also, it will be null if a redirection occurs (e.g. when a user logs into the web page).
This property is based on the HTTP_REFERER variable, so you could use this one instead.
Request.ServerVariables["HTTP_REFERER"]
Since the HTTP_REFERER is a variable sent by the client it might be altered or removed by the request. Also, the variable is not set when the referre url starts with https.
This article mentions a few points why the Request.UrlReferrer can be null.
Usually you use a query string parameter to achieve this: current?previousUrl=/some/11.
This will allow you to access this value from the server-side code using Context.Request.QueryString["previousUrl"] in your master page code-behind.
string urlName = Request.UrlReferrer.ToString();

How to get the site url with bookmark in c#

I want to get the absolute url of .net application with bookmark.
So if the request is coming from - www.mysite.com/myapplication/Home/Index#about
then how to get the above url in .net application?
I tried HttpContext.Request.Url.AbsoluteUri but it is returning - www.mysite.com/myapplication/Home/Index but how to get bookmark ("#about") part of url?
It is not possible: anything after the # is not sent by the browser, it is only used inside the loaded page for navigating to anchors.
The browser doesn't transmit that part to server.you should use java Script like this answer
Try this,
assuming that the variable url holds your actual url
string[] bookmark = url.Split('#');
and the bookmark[1] will hold the Value of your bookmark, also.
string bookmark = url.Replace(HttpContext.Request.Url.AbsoluteUri(url), string.Empty);

How to encode the redirect_uri for facebook login

I'm trying to build a login link for facebook and I'm I'm getting errors only in some cases. I'm trying to specify a a querystring parameter in redirect_uri token so that I can redirect them back to a specific area of my site after logging in. Here's what works and what doesn't work.
&redirect_uri=http://mydomain.com/login?returnUrl=returnUrl - works
&redirect_uri=http://mydomain.com/login?returnurl=/return/url -doesn't work
&redirect_uri=http%3a%2f%2fmyagentcheckin.com%2flogin%3freturnUrl%3d%2freturn%2furl -doesn't work
It seems that the / in the querystring are causing it to fail. Facebook returns an error when I try it. Anyone know of a way around this?
Instead of including the returnUrl parameter as part of your redirect_uri value, use the state parameter to store this data.
For instance,
https://graph.facebook.com/oauth/authorize?type=web_server&client_id={appid}&redirect_uri=http://www.yoursite.com/oauth/handshake&state=/requested/page
I have experienced something similar, especially with multiple redirects as above.
My solution is to put the returnUrl into the user's session (or perhaps a cookie), so I don't have to wrestle with double-encoding. For the redirect_url, just omit the querystring.
Try using this API that put together. It will remove the hassle of this for you.
No url encoding necessary.
Sample Authentication
Imports Branches.FBAPI
...
Dim SI As New SessionInfo("[application_id]","applicaiton_secret")
SI.AuthenticateUser("http://[my url]", New SessionInfo.PermissionsEnum(){SessionInfo.PermissionsEnum.email, SessionInfo.PermissionsEnum.read_stream}))
Read the response from the URL you provided above from that page.
Dim FSR = FS.ReadFacebooAuthResponse
When I tried what you do, I got a redirect callback something like this.
http://mydomain.com/login?returnurl=%2Freturn%2Furl&code=...
And I decode the "returnurl" value.
Then it worked fine for me.

Remove QueryString Variable in c#

I am very beginner With Programming...(unfortunately)
I want to remove Any added QueryString To Address after i get the variables. for example:
www.websiteName.com/page.aspx?a=344&b=233
i will get a and b and after that i want my address to look like this:
(www.websiteName.com) .
"root location".
any help...
thanks.
var queryString = Request.QueryString;
// Handle querystring, then redirect to root
Response.Redirect("~/");
Response.End();
You will have to reload the page. When changing the URL, you are making another request to the server.
I wrote a blog on retrieving the URL of an ASP.Net application. Simply add the page name after the result.
This blog describes how to manipulate the query string to redirect to the same page with different (or no) parameters.
Why not just use
Request.UserHostName

Categories