In c# MVC4 how to I get the server path. for example: http://192.169.1.120:60632
Is there a helper function that I can convert something like ~/aFolder/file.htm into an absolute path? Ideally I would like a way of taking any given url and convert into a full absolute url. E.g. can cope with..
/aFolder/file.html -> http://192.169.1.120:60632/aFolder/file.html
http://website.com/file.html -> http://website.com/file.html
And will work anywhere within the c# code - i.e. in a action controller, signalR hub, model etc.
And it will still work when I deploy to a remote server.
Use the Request.Url property available in Controller. This returns a Uri object containing info on the request. From there, you can access the AbsoluteUri and Port properties to get the info that you need.
If you are interested in getting the url info from SignalR, try looking at this question and answer.
Try this one,
System.Web.HttpContext.Current.Server.MapPath(#"~/FolderName")
You can try these as well
string path = AppDomain.CurrentDomain.GetData("FolderName").ToString();
HostingEnvironment.MapPath(#"~/FoldeName");
In MVC, you can get a fully qualified URL using the fourth parameter of Url.Action - protocol:
Url.Action("Index", "Home", null, Request.Url.Scheme)
Related
General info:
I am working on an ASP.NET MVC 5 application that sends emails and tracks link-clicking from within each email. When some user follows a link, they performs a GET query to my web server with certain query parameters. Meanwhile, my application saves information about the click and redirects user to the initial (target) site.
Closer to the point:
The links in mails may be arbitrary (e.g. http://www.google.com, //google.com, www.google.com) or even not valid (e.g. ab//cd). My application doesn't care about the string values of urls (even if it's like \asdf//sadg:) and will simply paste them directly to the address bar.
For this purpose I tried to use Redirect and RedirectPermanent methods, and the UriBuilder class. Redirect and RedirectPermanent may combine addresses and try to redirect to the local path if the url is specified without a scheme. UriBuilder manages to add a scheme if needed, but it throws exceptions when the url is invalid. JavaScript location.replace has the same behavior as c# Redirect, by the way.
The issue:
How can I redirect to arbitrary urls from my ASP.NET MVC controller without any validations and redirects to local paths?
You should be able to use the Redirect() method and just pass in a string. I don't think it tries to validate the given URI as long as you pass it in as a string.
Tested and "works" (Browser receives the correct location header):
return Redirect("\asdf//sadg:");
If you want to do this "manually", you could do something like this to perform the redirect by yourself:
Response.AddHeader("Location","whateverurl");
HttpContext.Response.StatusCode = (int)HttpStatusCode.Moved;
return new ContentResult() { Content = String.Empty };
Is there any way that I get the host address with the Application Name in IIS of my websites? To make my question clear here are few examples of what I want:
http://example.com/constructionSite/myController/myAction?blah=blahh
http://example.com/tr.Implementation/myOtherController/myAction?blah=blahh
http://example.com/anotherApplication/myController/myAction?blah=blahh
http://example.com/myOtherOtherController/myAction?blah=blahh
In my IIS I have applications like those above constructionSite, tr.Implementation, anotherApplication, myController. I need to get the urls with the application names on IIS, in other words up until the myController/...... And myController will not allways be the same controller, so I cannot use .IndexOf() method.
I tried playing with System.Web.HttpContext.Current.Request.Url.AbsoluteUri, System.Web.HttpContext.Current.Request.Url.AbsolutePath these paths, but it doesn't satisfy me.
What should be the appropriate approach to get the url with application name?
you can try using Url.Content("~")
#string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))
I am stuck at an unexpected issue in my project. The issue is that there is a URL produced on the fly in my code that I have to submit it to a RESTful web service via a GET request. For e.g. the URL to submit looks like this: http://mysampleserver.com:8080/calc/8999/bpaX
The RESTful server accepts URL as its last parameter in the format below:
http://myRestfulAPI.domainname.com/capture/bbbb/http://mysampleserver.com:8080/calc/8999/bpaX
I also used System.Net.HttpUtility.UrlEncode(....) to encode the "URL to submit" first to incorporate it in the RESTful service call.
That resulted in getting the error below:
System.Web.HttpException: A potentially dangerous Request.Path value was detected from the client (:)
To try to resolve it, I followed the steps described per this web page but no luck.
I am using MVC 4 to implement the RESTful API in C#.
Any clue or idea how to get around this showstopper issue?
There are at least two solutions I can think of.
Change your RESTFul service to use post, because you send information to your server, and potentially it will change your resource status, based on HTTP protocol , you should use POST anyway.
You can also encode your url with Base64
The steps that you've tried are the correct steps. See also this question potentially dangerous... which is the same issue.
There are a number of characters that .NET doesn't allow in in a URL by default, and the : is one of them (as a query string, at least). They are 'potentially dangerous'. Making this change to the configuration file allows these characters to be passed through to your application.
You need to Url.Encode the url in the query string (mvc parameters) otherwise it is interpreted as more URL encoding for MVC to decode as parameters. Try something like #Url.Encode(yourStringObject) and pass it as the last value or as a query (i.e. &q=url)
The incoming URL in this application has some URL parms but when I put a breakpoint on the base page, all I see is the internally UIP mapped URL which loses the parm information.
So, for UIP based applications what is the method to retrieve the original URL?
I tried Request.Url.AbsoluteUri but it just gives the ../index.aspx url instead of the non-templated url which is incoming.
Please try using the property Request.Url.OriginalString;
The property gets the original URI string that was passed to the Uri constructor.
I'm having trouble with paths in a ASP.NET MVC application that's running behind a proxy.
Our IIS Application root path is for example http://server/MyApp/
meaning that all urls using the application root ("~/",Url.Action("MyAction","MyController")) are resolved to "/MyApp"
Now we're running behind a proxy server that forwards all requests, but requires you to access the application through a URL like this:
"/Secury/Proxy/RubbishUrl/MyApp"
Because the proxy url is only available on the client, I thought of creating a cookie with the path prefix, and insert this before each generated URL on the server.
Now the question is, what's the best location in code to modify each URL that's resolved/sent to the client (to resources, controller actions, images etc)?
Every path in the application is resolved with the MVC methods (Url.Content, Url.Action etc).
Update:
Not actively looking for an answer anymore (though still interested in a proper solution)
Most of the time Proxies do their own URL translation, however in this case the proxy server is ignoring paths that are transfered in JSON, and they are processed.
My 'solution' for now is just not passing paths in JSON, but instead:
using proper ID's and values in the JSON requests
creating template in URL's in the HTML (which are resolved properly),
replace the ID's and values in the URL template with the values from the JSON requests
This method is actually a much 'cleaner' way IMO then passing the URL's.
You can create your own asp.net mvc controller factory where to decide which controller and action will serve the response based on the requested url. Check this url for a good blog post on how to do this - http://nayyeri.net/custom-controller-factory-in-asp-net-mvc.