Get current domain in ASMX - c#

In C# in an asmx web service how do I get the current domain that the webservice was called on?
HttpContext.Current.Request.Url.Host returns kindof what I want but instead of http://mydomain.com/Folder/Mywebservice.asmx I just need http://mydomain.com.
I know i could just cut that string up but it seems really in-elegant.
Thanks

Uri.GetLeftPart helps here:
Request.Url.GetLeftPart(UriPartial.Authority)

In VB.Net I have used...
With HttpContext.Current.Request.Url
sDomain=.Scheme & System.Uri.SchemeDelimiter & .Host
End With
Or if you care about the Port then...
With HttpContext.Current.Request.Url
sDomain=.Scheme & System.Uri.SchemeDelimiter & .Host & IIf(.IsDefaultPort,"",":") & .Port
End With
Should be easy to convert to C# ;)

Related

C# Url.Action parameters - Sub application

I am working on sub application related changes in web app. My web application is optimized to run with or without virtual directory. For that, I met with a condition that checked whether the incoming URL is having "/" or "/{subapplicationname}/"
To check this case I made a condition like below,
HttpContext.Request.Url.AbsolutePath == Url.Action("/", "/")
Without Subapplication: "/" == "/"
With Subapplication: "/applicationname/" == "/applicationname/"
Well actually, both cases (with and without sub application) passed.
And my doubt is whether Url.Action("/", "/") syntax is correct? or else going for HttpRuntime.AppDomainAppVirtualPath is optimal one?
I have searched over internet and didn't find Url.Action("/", "/") scenario.
Any help is appreciated. Thanks in advance.

Change MTU Programmatically with C#

I want to change MTU value for windows 7/8.1/10 with C#.
I tried to search on Stack Overflow but only netsh is what I can find.
Get current MTU value
Set custom MTU value
I don't want to use any cmd commands, any idea to do with C# only?
You should be able to use the WMI API for this.
There's a Visual Basic example you can probably adapt:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
objNetworkSettings.SetMTU(68)
The SetMTU method is documented here, and the C# APIs to talk with WMI are documented here.

Equivalence of CreateObject("Microsoft.XMLHTTP")

I got some code from our provider which is written in VB. I need this code to be converted to C#.
Below if the VB code i need to convert to C#.
Dim VLINK_URL As String
VLINK_URL = "http://service.xxx.com/report?type=" & REPORT_TYPE & "&vin=" & vinNumber
Dim GetConnection As Object
GetConnection = CreateObject("Microsoft.XMLHTTP")
GetConnection.Open("get", VLINK_URL, False, USERID, PASS)
GetConnection.Send()
Where USERID and PASS are properties of the class to set the credentials.
The exact problem is with CreateObject("Microsoft.XMLHTTP").
Can somebody help me resolve this issue.
Thanks for taking your valuable time.
Look up WebRequest.
See examples section.
Use Type.GetTypeFromProgID:
Type xmlType = Type.GetTypeFromProgID("Microsoft.XMLHTTP");
dynamic objXML = Activator.CreateInstance(objBLType);
objXML.Open("get", VLINK_URL, False, USERID, PASS)
objXML.Send()
However, I would take a step back and understand what you're trying to do - the XMLHTTP COM library is at nearly 15 years old now and existed before .NET had better support for web serivces (assuming that's what you're using the library for).

get main part of url including virtual directory

I am working with .net 4.0 c#.
I want to be able to get the url from the current http request, including any virtual directory. So for example (request and sought value):
http://www.website.com/shop/test.aspx -> http://www.website.com/shop/
http://www.website.com/test.aspx -> http://www.website.com/
http://website.com/test.aspx -> http://website.com/
How is it possible to achieve this?
This is what I use
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
Request.Url should contain everything you need. At that point it's a matter of checking the string, and what you prefer to grab from it. I've used AbsoluteUri before, and it works.
This example isn't fool proof, but you should be able to figure out what you need from this:
string Uri = Request.Url.AbsoluteUri;
string Output = Uri.Substring(0, Uri.LastIndexOf('/') + 1 );
This solution could work and is shorter:
string url = (new Uri(Request.Url, ".")).OriginalString;
This should work
Request.Url.OriginalString.Substring(0, Request.Url.OriginalString.LastIndexOf(Request.FilePath.Substring(Request.FilePath.LastIndexOf("/")))) + "/"

Does C# have an equivalent to JavaScript's encodeURIComponent()?

In JavaScript:
encodeURIComponent("©√") == "%C2%A9%E2%88%9A"
Is there an equivalent for C# applications? For escaping HTML characters I used:
txtOut.Text = Regex.Replace(txtIn.Text, #"[\u0080-\uFFFF]",
m => #"&#" + ((int)m.Value[0]).ToString() + ";");
But I'm not sure how to convert the match to the correct hexadecimal format that JS uses. For example this code:
txtOut.Text = Regex.Replace(txtIn.Text, #"[\u0080-\uFFFF]",
m => #"%" + String.Format("{0:x}", ((int)m.Value[0])));
Returns "%a9%221a" for "©√" instead of "%C2%A9%E2%88%9A". It looks like I need to split the string up into bytes or something.
Edit: This is for a windows app, the only items available in System.Web are: AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel.
Uri.EscapeDataString or HttpUtility.UrlEncode is the correct way to escape a string meant to be part of a URL.
Take for example the string "Stack Overflow":
HttpUtility.UrlEncode("Stack Overflow") --> "Stack+Overflow"
Uri.EscapeUriString("Stack Overflow") --> "Stack%20Overflow"
Uri.EscapeDataString("Stack + Overflow") --> Also encodes "+" to "%2b" ---->Stack%20%2B%20%20Overflow
Only the last is correct when used as an actual part of the URL (as opposed to the value of one of the query string parameters)
HttpUtility.HtmlEncode / Decode
HttpUtility.UrlEncode / Decode
You can add a reference to the System.Web assembly if it's not available in your project
I tried to do full compatible analog of javascript's encodeURIComponent for c# and after my 4 hour experiments I found this
c# CODE:
string a = "!##$%^&*()_+ some text here али мамедов баку";
a = System.Web.HttpUtility.UrlEncode(a);
a = a.Replace("+", "%20");
the result is:
!%40%23%24%25%5e%26*()_%2b%20some%20text%20here%20%d0%b0%d0%bb%d0%b8%20%d0%bc%d0%b0%d0%bc%d0%b5%d0%b4%d0%be%d0%b2%20%d0%b1%d0%b0%d0%ba%d1%83
After you decode It with Javascript's decodeURLComponent();
you will get this:
!##$%^&*()_+ some text here али мамедов баку
Thank You for attention
System.Uri.EscapeUriString() didn't seem to do anything, but System.Uri.EscapeDataString() worked for me.
Try Server.UrlEncode(), or System.Web.HttpUtility.UrlEncode() for instances when you don't have access to the Server object. You can also use System.Uri.EscapeUriString() to avoid adding a reference to the System.Web assembly.
For a Windows Store App, you won't have HttpUtility. Instead, you have:
For an URI, before the '?':
System.Uri.EscapeUriString("example.com/Stack Overflow++?")
-> "example.com/Stack%20Overflow++?"
For an URI query name or value, after the '?':
System.Uri.EscapeDataString("Stack Overflow++")
-> "Stack%20Overflow%2B%2B"
For a x-www-form-urlencoded query name or value, in a POST content:
System.Net.WebUtility.UrlEncode("Stack Overflow++")
-> "Stack+Overflow%2B%2B"
You can use the Server object in the System.Web namespace
Server.UrlEncode, Server.UrlDecode, Server.HtmlEncode, and Server.HtmlDecode.
Edit: poster added that this was a windows application and not a web one as one would believe. The items listed above would be available from the HttpUtility class inside System.Web which must be added as a reference to the project.

Categories

Resources