I'm request for get url :
public Uri GetAbsoluteUri()
{
var request = _httpContextAccessor.HttpContext.Request;
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = request.Scheme;
uriBuilder.Host = request.Host.Host;
uriBuilder.Path = request.Path.ToString();
uriBuilder.Query = request.QueryString.ToString();
return uriBuilder.Uri;
}
public string RootPath => Path.Combine(WebRootPath, RootFolderName);
public string GetProductPicturePath()
{
return Path.Combine(GetAbsoluteUri().ToString(), RootFolderName, ProductPictureFolder);
}
public string GetProductMainPicturePath()
{
string path = Path.Combine(GetAbsoluteUri().ToString(), RootFolderName, ProductPictureFolder, ProductMainPictureFolder);
return path;
}
public string GetNewPath()
{
string productMainPicturePath = GetProductMainPicturePath();
return Path.Combine(productMainPicturePath);
}
finally i using the GetNewPath().
, but this will give me the address :
https://localhost/api/Product/GetProductList/Upload/ProductPictureFolder/ProductMainPicture/77777.png
but i have 2 problem with this url :
1 - it not contain port in url https://localhost/api but i need return like this : http://localhost:4200/api
2 - This includes the name of the controller and the ActionName, but I need to like this : https://localhost/Upload/ProductPictureFolder/ProductMainPicture/77777.png
but it return for me this : https://localhost/api/Product/GetProductList/Upload/ProductPictureFolder/ProductMainPicture/77777.png
i not need this /api/Product/GetProductList .
Product : Controller Name
GetProductList : ActionName
How Can I Solve This Problem ???
1 - it not contain port in url https://localhost/api but i need return like this
To get port you can use this snippet :
if (request.Host.Port.HasValue)
uriBuilder.Port = request.Host.Port.Value;
2 - This includes the name of the controller and the ActionName, but I
need to like this :
https://localhost/Upload/ProductPictureFolder/ProductMainPicture/77777.png
I suggest you that set UriBuilder Path based on your needs, not from the request. Something like this:
// Make your Upload file path here
var relativePath = Path.Combine(folderName, filename);
var request = _httpContextAccessor.HttpContext.Request;
var uriBuilder = new UriBuilder
{
Host = request.Host.Host,
Scheme = request.Scheme,
Path = relativePath
};
if (request.Host.Port.HasValue)
uriBuilder.Port = request.Host.Port.Value;
var imageUrl = uriBuilder.ToString();
I'm using C#, .Net Framework 4.5.1 in SharpDevelop.
Is there a way to create a function that accepts "typeof(ISomeInterface)" and "typeof(SomeClass)" as parameters?
Situation:
I'm building an application, that is supposed to host several WebServices. Each WebService will be created on it's own System.ServiceModel.ServiceHost. Currently, there are only two WebServices running (Tester1 and Tester2, as I'm just trying to get it to work).
In the future, there will be multiple WebServices running in this application, perhaps even "unknown", but derived from my bases. My current setup works, but, as you can see in the code below, is far from being the most DRY-style code. So, naturally, I would like to move all repeating code into a method.
Currently, I have the following code:
First, I have a base-interface and a base-class:
[ServiceContract]
public interface ITester
{
[OperationContract]
void Test( string pText );
}
public class Tester : System.Web.Services.WebService, ITester
{
public Tester( )
{
}
public virtual void Test( string pText )
{
}
}
From which I have derived two child interfaces and classes:
[ServiceContract]
public interface ITester1 : ITester
{
}
public class Tester1 : Tester, ITester1
{
public override void Test( String pMessage )
{
Console.WriteLine( "Message received : \n{0}\n", pMessage );
}
}
[ServiceContract]
public interface ITester2 : ITester
{
}
public class Tester2 : Tester, ITester2
{
public Tester2( )
{
}
public override void Test( string pText )
{
Console.WriteLine( "Message received : \n{0}\n", pText );
}
}
At current, the code below creates and runs the WebServices, based on the above interfaces and classes:
public class Program
{
public static void Main( )
{
int lWhatService = 1; // Actual Program uses user input.
if( lWhatService == 1 )
{
string iServer = "localhost";
int iPort = 80;
string iService = "Tester1";
string iWDSL = "wsdl";
string iHTTPAddress = "http://" + iServer + ":" + iPort.ToString( ) + "/" + iService;
string iMetaAddress = "http://" + iServer + ":" + iPort.ToString( ) + "/" + iService + "/" + iWDSL;
string iNetTCPAddress = "net.tcp://" + iServer + "/" + iService + "/" + iWDSL;
ServiceHost lHost;
Uri iHTTPUri = new Uri( iHTTPAddress );
BasicHttpBinding binding = new BasicHttpBinding( );
lHost = new ServiceHost( typeof(Tester1) );
lHost.AddServiceEndpoint( typeof(ITester1), binding, iHTTPUri );
ServiceMetadataBehavior metadata = new ServiceMetadataBehavior( );
metadata.HttpGetUrl = new Uri( iMetaAddress );
metadata.HttpGetEnabled = true;
lHost.Description.Behaviors.Add( metadata );
Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding( );
Uri mexAddress = new Uri( iNetTCPAddress );
lHost.AddServiceEndpoint( typeof(IMetadataExchange), mexBinding, mexAddress );
lHost.Open( );
}
else if( lWhatService == 2 )
{
string iServer = "localhost";
int iPort = 80;
string iService = "Tester2";
string iWDSL = "wsdl";
string iHTTPAddress = "http://" + iServer + ":" + iPort.ToString( ) + "/" + iService;
string iMetaAddress = "http://" + iServer + ":" + iPort.ToString( ) + "/" + iService + "/" + iWDSL;
string iNetTCPAddress = "net.tcp://" + iServer + "/" + iService + "/" + iWDSL;
ServiceHost lHost;
Uri iHTTPUri = new Uri( iHTTPAddress );
BasicHttpBinding binding = new BasicHttpBinding( );
lHost = new ServiceHost( typeof(Tester2) );
lHost.AddServiceEndpoint( typeof(ITester2), binding, iHTTPUri );
ServiceMetadataBehavior metadata = new ServiceMetadataBehavior( );
metadata.HttpGetUrl = new Uri( iMetaAddress );
metadata.HttpGetEnabled = true;
lHost.Description.Behaviors.Add( metadata );
Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding( );
Uri mexAddress = new Uri( iNetTCPAddress );
lHost.AddServiceEndpoint( typeof(IMetadataExchange), mexBinding, mexAddress );
lHost.Open( );
}
System.Console.ReadKey( ); // This is where the user normally could switch
}
}
As can be seen, the two methods (Run1 and Run2) are about 90% identical, except that they use ITester1 & Tester1 or ITester2 & Tester2.
Now, I have replaced some code:
lHost = new ServiceHost( typeof(Tester1) );
lHost.AddServiceEndpoint( typeof(ITester1), binding, iHTTPUri );
and
lHost = new ServiceHost( typeof(Tester2) );
lHost.AddServiceEndpoint( typeof(ITester2), binding, iHTTPUri );
By some more "generic" code:
Type lObj = typeof(Tester1);
lHost = new ServiceHost( lObj );
Type lInt = typeof(ITester1);
lHost.AddServiceEndpoint( lInt, binding, iHTTPUri );
and
Type lObj = typeof(Tester2);
lHost = new ServiceHost( lObj );
Type lInt = typeof(ITester2);
lHost.AddServiceEndpoint( lInt, binding, iHTTPUri );
This, in preparation of moving the code to a method. The code still works, and I still get the response (currently, just the WebServices WSDL) I expect.
The obvious next step, would be to move all code in the if( lWhatService == 1 ) and else if( lWhatService == 2 ) blocks to a single method.
I have used several ways, but below is where I currently am:
public static ServiceHost GetHost<T, U>( string pServer, int pPort, string pService, string pWSDL )
{
string iHTTPAddress = "http://" + pServer + ":" + pPort.ToString( ) + "/" + pService;
string iMetaAddress = "http://" + pServer + ":" + pPort.ToString( ) + "/" + pService + "/" + pWSDL;
string iNetTCPAddress = "net.tcp://" + pServer + "/" + pService + "/" + pWSDL;
Type iInterface = typeof(T);
Type iObject = typeof(U);
ServiceHost iHost = new ServiceHost( iObject );
// HTTP Binding => Create and Prepare
Uri iHTTPUri = new Uri( iHTTPAddress );
BasicHttpBinding iHTTPBinding = new BasicHttpBinding( );
// HTTP Binding => Add to Host
iHost.AddServiceEndpoint( iInterface, iHTTPBinding, iHTTPAddress );
....
....
return iHost;
}
The above code, I would like to call as:
lHost1 = GetHost<ITester1, Tester1>( "localhost", 80, "Tester1", "wsdl" );
lHost1.Open( );
lHost2 = GetHost<ITester2, Tester2>( "localhost", 80, "Tester2", "wsdl" );
lHost2.Open( );
The code works, in the sense that I do not get any compile-time or run-time errors. I also get a response from the application. However, in my previous way (coding both WebServices completely) I got the WSDL-XML as a response. In the current way (calling a method) I get and empty page (not an error, just a blanc page).
I have also tried these before:
public static ServiceHost GetHost( string pServer, int pPort, Type pInterface, Type pObject, string pService, string pWSDL )
{
Type iInterface = typeof(pInterface);
Type iObject = typeof(pObject);
ServiceHost iHost = new ServiceHost( iObject );
....
}
and
public static ServiceHost GetHost( string pServer, int pPort, Type pInterface, Type pObject, string pService, string pWSDL )
{
Type iInterface = pInterface;
Type iObject = pObject;
ServiceHost iHost = new ServiceHost( iObject );
....
}
Calling them like:
lHost = GetHost( "localhost", 80, typeof(ITester1), typeof(Tester1), "Tester1", "wsdl" );
and
lHost = GetHost( "localhost", 80, ITester1, Tester1, "Tester1", "wsdl" );
But that got me an empty page just the same.
Is there any way to get the above work, so I can call it either as:
lHost1 = GetHost<ITester1, Tester1>( "localhost", 80, "Tester1", "wsdl" );
or
lHost1 = GetHost( "localhost", 80, typeof(ITester1), typeof(Tester1), "Tester1", "wsdl" );
or (most preferred)
lHost1 = GetHost( "localhost", 80, ITester1, Tester1, "Tester1", "wsdl" );
I have tried these and about 9 more variations (suggestion from over 20 internet pages), but I just cannot seem to get this to work.
Any help would be greatly appreciated.
Found it, and I feel so ashamed for not seeing it earlier. My method missed one important line: iMeta.HttpGetEnabled = true; With it, it works fine. Should be place (in the method) directly after: iMeta.HttpGetUrl = iMetaUri;
Thanks goes to:
#cubbr: Thanks for making take a second look at my code.
#Preston Guillog: Thanks for making me think a bit more about what I was doing.
The final function now looks like:
public static ServiceHost GetHost( string pServer, int pPort, Type pInterface, Type pObject, string pService, string pWSDL )
{
string iHTTPAddress = "http://" + pServer + ":" + pPort.ToString( ) + "/" + pService;
string iMetaAddress = "http://" + pServer + ":" + pPort.ToString( ) + "/" + pService + "/" + pWSDL;
string iNetTCPAddress = "net.tcp://" + pServer + "/" + pService + "/" + pWSDL;
Type iInterface = pInterface;
Type iObject = pObject;
ServiceHost iHost = new ServiceHost( iObject );
// HTTP Binding => Create and Prepare
Uri iHTTPUri = new Uri( iHTTPAddress );
BasicHttpBinding iHTTPBinding = new BasicHttpBinding( );
// HTTP Binding => Add to Host
iHost.AddServiceEndpoint( iInterface, iHTTPBinding, iHTTPAddress );
// MetaData => Create and Prepare
Uri iMetaUri = new Uri( iMetaAddress );
ServiceMetadataBehavior iMeta = new ServiceMetadataBehavior( );
iMeta.HttpGetUrl = iMetaUri;
iMeta.HttpGetEnabled = true;
// MetaData => Add to Host
iHost.Description.Behaviors.Add( iMeta );
Uri iNetTCPUri = new Uri( iNetTCPAddress );
Binding iNetTCPBinding = MetadataExchangeBindings.CreateMexTcpBinding( );
iHost.AddServiceEndpoint( typeof( IMetadataExchange), iNetTCPBinding, iNetTCPUri );
return iHost;
}
and can be called like:
lHost = GetHost( "localhost", 80, typeof(ITester1), typeof(Tester1), "Tester1", "wsdl" );
lHost.Open( );
I'm new to C#,
lets say I have a string
string testurl = "http://www.mytestsite.com/hello";
if (test url == root domain) {
// do something
}
I want to check if that string "testurl" is the root domain i.e http://www.mytestsite.com or http://mytestsite.com etc.
Thanks.
Use the Uri class:
var testUrl = new Uri("http://www.mytestsite.com/hello");
if (testUrl.AbsolutePath== "/")
{
Console.WriteLine("At root");
}
else
{
Console.WriteLine("Not at root");
}
Which nicely deals with any normalization issues that may be required (e.g. treating http://www.mytestsite.com and http://www.mytestsite.com/ the same)
You may try like this:
string testurl = "http://www.mytestsite.com/hello"
if ( GetDomain.GetDomainFromUrl(testurl) == rootdomain) {
// do something
}
You can also try using URI.HostName property
The following example writes the host name (www.contoso.com) of the server to the console.
Uri baseUri = new Uri("http://www.contoso.com:8080/");
Uri myUri = new Uri(baseUri, "shownew.htm?date=today");
Console.WriteLine(myUri.Host);
If the hostname returned is equal to "http://mytestsite.com" you are done.
string testurl = "http://www.mytestsite.com/hello";
string prefix = testurl.Split(new String[] { "//" })[0] + "//";
string url = testurl.Replace(prefix, "");
string root = prefix + url.Split("/")[0];
if (testurl == root) {
// do something
}
Introduction:
I have a start url suppose www.example.com now i run scraper on this url to collect all the internal links belonging to same site and external links.
Problem:
I am using the code below to compare a found url with the main url www.example.com to see if they both have same domain so i take the url as internal url.
Uri baseUri = new Uri(url); //main URL
Uri myUri = new Uri(baseUri, strRef); //strRef is new found link
//domain = baseUri.Host;
domain = baseUri.Host.Replace("www.", string.Empty).Replace("http://", string.Empty).Replace("https://", string.Empty).Trim();
string domain2=myUri.Host.Replace("www.", string.Empty).Replace("http://", string.Empty).Replace("https://", string.Empty).Trim();
strRef = myUri.ToString();
if (domain2==(domain) )
{ //DO STUFF }
Is the above the correct logic ? Because if supposing i get a new url http://news.example.com the domain name found becomes : news.example.com which does not match the domain name of the main url . Is this correct?Should it match or not . And What is a better way if mine is not good enough.
here is a solution to find main domain from subdomain
string url = "http://www.xxx.co.uk";
string strRef = "http://www.news.xxx.co.uk";
Uri baseUri = new Uri(url); //main URL
Uri myUri = new Uri(baseUri, strRef); //strRef is new found link
var domain = baseUri.Host;
domain = baseUri.Host.Replace("www.", string.Empty).Replace("http://", string.Empty).Replace("https://", string.Empty).Trim();
//hrere is solution
string domain2 = GetDomainName(strRef);
strRef = myUri.ToString();
if (domain2 == (domain))
{ //DO STUFF
}
private static string GetDomainName(string url)
{
string domain = new Uri(url).DnsSafeHost.ToLower();
var tokens = domain.Split('.');
if (tokens.Length > 2)
{
//Add only second level exceptions to the < 3 rule here
string[] exceptions = { "info", "firm", "name", "com", "biz", "gen", "ltd", "web", "net", "pro", "org" };
var validTokens = 2 + ((tokens[tokens.Length - 2].Length < 3 || exceptions.Contains(tokens[tokens.Length - 2])) ? 1 : 0);
domain = string.Join(".", tokens, tokens.Length - validTokens, validTokens);
}
return domain;
}
I am using ASP Net C#.
I have nested cookie that I am updating in client.
my cookie :
OwnerId=2027&LastAppId=2203&AppType=4&Mail=simba#w.l
js :
var cookieName = "LastAppId"
var lastAppId = jQuery.getNestedCookie("myCookie", cookieName);
var lastQryAppId = "&" + cookieName + "=" + lastAppId;
var currentQryAppId = "&" + cookieName + "=" + newAppId;
var newFullCookieStr = fullCookie.replace(lastQryAppId, currentQryAppId);
jQuery.cookie("myCookie", newFullCookieStr, { expires: 360, path: '/' });
C#:
HttpContext.Current.Request.Cookies["myCookie"].Value ="OwnerId%3D2027%262LastAppId%3D2203%26AppType%3D4%26Mail%3Dsimba%40w.l"
but I cant get it by name beacause all keys are gone .
(HttpContext.Current.Request.Cookies["2min_Auth"].Values).AllKeys[0] = null;
i know I can take the string value and substring it or split and get the values but is any one know why its happen and how can I return the keys/values to work again ??
var s = HttpUtility.UrlDecode("OwnerId%3D2027%262LastAppId%3D2203%26AppType%3D4%26Mail%3Dsimba%40w.l");
NameValueCollection nameValues = HttpUtility.ParseQueryString(s);