How to get the site url with bookmark in c# - 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);

Related

Getting URL(URI) address from Web Browser

I am trying to get the URL string (uri) in the WebBrowser component after navigation, but the returned address is incomplete.
It should look like "https://oauth.vk.com/blank.html#access_token=..." but
e.Uri.AbsoluteUri
and all other fields return only "https://oauth.vk.com/blank.html" (image)
A line beyond the "#" is not returned. I navigate to the address in the browser, everything is displayed normally. I didn't find the answer to this question anywhere, I hope, here will help me.
I tried to get the URL in different ways but failed to get the full string.
Here's an code example:
browser.Navigate(getTokenUrl);
browser.Navigated += (sender, e) =>
{
MessageBox.Show(e.Uri.AbsoluteUri);
};
JavaScript
Window.location.href will return all the url including hash part.
You can use apis to run this JavaScript in browser component and it should return you correct url.
The api to run JavaScript depends on browser component you are using.

Can I use a format string in a URL to navigate to web page, replacing the specified portion of the URL?

I'm interested in creating some sort of simple C# application takes a user string and passes it into a target portion of the URL. For example, since user query is visible in the page URL DuckDuckGo
Example:
https://duckduckgo.com/?q=web+browsers&ia=web
In this case, the URL shows that I searched for "web browsers". I would like the user to be able to pass any string to the application (via some kind of prompt that appears with the application is launched), and then launch a web browser and navigate to the target URL with the user input inserted into URL where the query is specified. (i.e., https://duckduckgo.com/?q=operating+systems&ia=web), where the user entered the string "operating systems".
So I would like to know which type of C# application to use that can interact with OS (Windows 10) and how to write the code for the the format String and the user prompt. Any guidance would be appreciated.
Your question is very broad so the best that can be done is give a broad answer. You mention "application" and "interact with the OS", so I'm assuming a native application, not a web app. A quick way to pull this off would be to Google for "C# Web Browser Example"; there are plenty of applications out there with well-explained source code that will answer your question:
So I would like to know which type of C# application to use that can
interact with OS (Windows 10)
As for the string replacement, Armine already pointed that out in his previous post. A simple textbox on your form, passed to some parsing code with string replacement, will do the trick for building the URL. The resulting URL is then passed to the web browser control you've used in your C# application; the URL will be one of the properties of the control.
The idea is to take what user typed as a string, and then create another string which will contain the words of that string, separated by the plus character (+)
String what_user_typed=" javascript jquery";
String query=what_user_typed.Replace(" ","+"); // A space represents a new word
String url="https://www.google.com/search?query="+query
After creating the url you can then use a webbrowser to open that url
I have not executed this but think this is what the logic should be.
string input = "operating system";
string destinationURL = $"https://duckduckgo.com/?q={input}&ia=web";
string formattedURL = HttpContext.Current.Server.UrlEncode(destinationURL);
System.Diagnostics.Process.Start(formattedURL);

How to maintain the right URL in C#/ASP.NET?

I am given a code and on one of its pages which shows a "search result" after showing different items, it allows user to click on one of records and it is expected to bring up a page so that specific selected record can be modified.
However, when it is trying to bring up the page I get (by IE) "This page cannot be displayed".
It is obvious the URL is wrong because first I see something http://www.Something.org/Search.aspx then it turns into http://localhost:61123/ProductPage.aspx
I did search in the code and found the following line which I think it is the cause. Now, question I have to ask:
What should I do to avoid using a static URL and make it dynamic so it always would be pointing to the right domain?
string url = string.Format("http://localhost:61123/ProductPage.aspx?BC={0}&From={1}", barCode, "Search");
Response.Redirect(url);
Thanks.
Use HttpContext.Current.Request.Url in your controller to see the URL. Url contains many things including Host which is what you're looking for.
By the way, if you're using the latest .Net 4.6+ you can create the string like so:
string url = $"{HttpContext.Current.Request.Url.Host}/ProductPage.aspx?BC={barCode}&From={"Search"}";
Or you can use string.Format
string host = HttpContext.Current.Request.Url.Host;
string url = string.Format("{0}/ProductPage.aspx?BC={1}&From={2}"), host, barCode, "Search";
You can store the Host segment in your AppSettings section of your Web.Config file (per config / environment like so)
Debug / Development Web.Config
Production / Release Web.Config (with config override to replace the localhost value with something.org host)
and then use it in your code like so.
// Creates a URI using the HostUrlSegment set in the current web.config
Uri hostUri = new Uri(ConfigurationManager.AppSettings.Get("HostUrlSegment"));
// does something like Path.Combine(..) to construct a proper Url with the hostName
// and the other url segments. The $ is a new C# construct to do string interpolation
// (makes for readable code)
Uri fullUri = new Uri(hostUri, $"ProductPage.aspx?BC={barCode}&From=Search");
// fullUrl.AbosoluteUri will contain the proper Url
Response.Redirect(fullUri.AbsoluteUri);
The Uri class has a lot of useful properties and methods to give you Relative Url, AbsoluteUrl, your Url Fragments, Host name etc etc.
This should do it.
string url = string.Format("ProductPage.aspx?BC={0}&From={1}", barCode, "Search");
Response.Redirect(url);
If you are using .Net 4.6+ you can also use this string interpolation version
string url = $"ProductPage.aspx?BC={barcode}&From=Search";
Response.Redirect(url);
You should just be able to omit the hostname to stay on the current domain.

How to get current request url in Sharepoint 2013 in 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

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