Just having a problem trying to properly display an image from an external site but mvc constraints links and automatically adds the localhost: url at the start of everything even with custom routing this cannot be avoided
eg I require: www.google.com/finance/chart?q=NYSE:V&tlf=12
but i am getting: http://localhost:3022/www.google.com/finance/chart?q=NYSE:V&tlf=12
any help would be much appreciated
Your problem is not MVC; it is the formation of your <a> tags. You are doing it like this:
blah...
You should be doing it like this:
blah...
Without including the protocol at the beginning, the browser assumes your link is relative to the current site. It has nothing whatsoever to do with MVC.
If you require a link on a separate domain, you need to add http://
So :
http://www.google.com/finance/chart?q=NYSE:V&tlf=12
Should work!
Why? Without the http, the link is considered relative and the browser uses the relative domain -> localhost!
Related
I have URLs stored in a database where the spaces are encoded as +.
When the browser requests these urls the web server returns a 404 response.
These URLs are all for static images stored on the web server in the wwwroot folder.
If I manually change the + for %20 then the image is returned correctly.
Is this a deliberate change in ASP.Net Core or is this a bug?
If it's deliberate, then it's going to be very painful for me going through the database and re-encoding all the URLs, many of which are embedded in HTML snippets (I know storing HTML in the DB or having spaces in image files aren't a good idea but it was done long before I joined the company and that's the state we're already in).
I'm using ASP.Net 2.1, running on .Net Framework.
It's running through IIS Express at the moment (during development) but will be deployed with full IIS.
I have seen this other question but it's specifically to do with API calls and the answer doesn't seem to be applicable to my question as there are no routes to change as I'm requesting static image files.
Edit: Extra detail
The html is output using #Html.Raw(html)
The resulting html output to the browser is of the form
<img src="/BorderThemes/grey+4px+rounded+corners_TL.png" />
The Html was generated on the server and then stored in the DB so we can be confident it's safe to output to the browser and, no, I have no idea why anyone would do that rather than building the HTML when it's needed but it was before my time and it's the situation I'm already in.
Update:
I've looked deeper into this and if I enter http://localhost:8000/BorderThemes/grey+4px+rounded+corners_TL.png into my web browser I get a page from IIS saying Http Error 404.11 saying that my URL is double encoded and linking to here for more information. This does include instructions on how to allow double-encoding but with warnings that it can have security consequences.
If I enter the url http://localhost:8000/BorderThemes/grey%204px%20rounded%20corners_TL.png I get an image back.
I was having issues with paths / html stored in the DB but after experimenting, it appears that System.Net.WebUtility.UrlEncode encodes spaces as +. For example WebUtility.UrlEncode("foo bar.png") returns foo+bar.png, which is rejected as double-encoded by IIS.
Am I missing something or is Microsoft's function for encoding URLs encoding the URLs in a way that Microsoft's web server rejects?
If you want %20 instead of + tryusing EscapeDataString to encode URI :
Uri.EscapeDataString(someString);
Refer https://stackoverflow.com/a/50682381/704008
But you have already generated url & can'e do anything now so try using HtmlDecode like
System.Net.WebUtility.HtmlDecode.HtmlDecode(html);
I am not sure it best to use with Raw or some method exists like decode in #Html but try using :
#Html.Raw(System.Net.WebUtility.HtmlDecode(html))
Refer:
https://learn.microsoft.com/en-us/dotnet/api/system.net.webutility?view=netstandard-2.0
https://learn.microsoft.com/en-us/dotnet/api/system.net.webutility.urldecode?view=netstandard-2.0#System_Net_WebUtility_UrlDecode_System_String_
I create a website by Visual Studio and publish it.
Then in IIS I do:
dd application and
set alias name to CIP and set physical path.
Home page is loaded correctly but other pages have wrong URLs in addressbar.
This is homepage URL: "http://localhost/CIP/Pages/Default.aspx".
When I click on the other link showed below link without CIP(alias name).
"http://localhost/Pages/OperationPersonelProgram.aspx".
You need to have your application make sure that the URLs it's using make sense. This requires some server side code to execute. So, if you're writing an MVC website you would use the UrlHelper class, something like:
<a class="link" href="#Url.Content("~/Pages/HostessPersonalProgram.aspx")">mylink</a>
(The above is Razor syntax, but similar should be possible).
Most of the different "styles" of ASP.Net offer a similar shorthand for obtaining the correct URL, but if all else fails you can always use VirtualPathUtility.ToAbsolute:
var url = VirtualPathUtility.ToAbsolute("~/Pages/HostessPersonalProgram.aspx");
how would i go about implementing something like the following...
example.com site has these pages:
example.com/id1/page1/
example.com/id1/page2/
example.com/id1/page3/
example.com/id2/page1/
example.com/id2/page2/
example.com/id2/page3/
and now i want to have that when i point domains example1.com and example2.com to example.com/id1/ page and example.com/id2/page1/ respectively.
the site is on azure and what i did was set a dns for all three domains (example.com, example1.com, example2.com) to point to the same ip.
and then on the home page of the site i do a redirect with
Response.Redirect(...);
but this means that the domains example1.com and example2.com are not seen in the browser url, but rather the urls such as example.com/id1/ and example.com/id2/ are seen instead.
what i would instead like is to have my site show as the actual domain in such a way that these URL's are never seen:
example.com/id1/
example.com/id1/page1/
example.com/id1/page2/
example.com/id1/page3/
example.com/id2/
example.com/id2/page1/
example.com/id2/page2/
example.com/id2/page3/
and instead they respectively show up as as
example1.com/
example1.com/profile/
example1.com/about/
example1.com/contact/
example2.com/
example2.com/profile/
example2.com/about/
example2.com/contact/
so what do i need to change to make the domains visible as such?
sorry for the long question :( hope you can help me.
You wouldn't do that using a redirect since, as you mentioned, you want to keep the URL displayed as the one typed in. What you are trying to do is URL routing. For ASP.NET, the process and configuration is explained here: http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx
If your list of pages is limited, you can do routing statically. Or you can provide the route table based on a database of pages or something. The walkthrough here explains how to provide those routes: http://msdn.microsoft.com/en-us/library/dd329551(v=vs.100).aspx
The Setup
I am building an app using ASP.NET MVC3, the application makes use of sub domains, i added the following in my hosts file : 127.0.0.1 students.localhost.
This all seems fine, when i debug, the browser opens up localhost:{PORT}, i can browse the site, i can also open up: students.localhost:{PORT}, and the site works perfectly.
In case you were wondering, i made use of: Maarten Balliauw's code to achieve the routing requirements in MVC and subdomains
The Problem
I need to somehow find out what subdomain the user is accessing the site from. If i debug, my and go to my subdomain:http://students.localhost:{PORT} Request.Url is : http://localhost:{PORT}, for some reason the deubugger (or ASP.NET Development Server) is not picking up students.
Please do not go into the TLD descussion trying to explain what a subdomain really is, all i need is the first string after http://. in local and production this WILL be my subdomain.
Thanx in advance
UPDATED:
I managed to get the desired result by making use of:Request.Headers["host"], it would be interesting to find out why Request.Url does not contain the students substring.
The easy way to do this is to put a fully qualified domain name in hosts. If the production site is subdomain.domain.com, I like to use subdomain.domain.local and just map this to 127.0.0.1.
new System.Uri(Request.RawUrl).Host
I think this will be the real hostname.
I am try to get routing configured on an asp.net 4.0 site running on an IIS6 server.
I am using MapPageRoute and it takes me to the correct page. Problems I have encountered so far:
Extensionless Url Routing
Solved by installing QFE described here Link
Static content such as js, css and images not displaying
Solved using Chris Cavanagh's baseUrl technique described here chriscavanagh.wordpress.com/2008/11/06/aspnet-routing-just-enough-rope/
Relative urls and postback scenarios
NOT SOLVED. For some reason, relative paths are not being interpreted correctly. For example. asp:ImageButton runat="server" ImageUrl="~/images/tree.jpg" is rendering out as and img with src="". I have been able to force it to work by setting the ImageUrl to an absolute path but I can't do this for everything. It is also affecting postback scenarios. Button clicks are taking me from (eg) localhost/website/articles/the-article_description/ to localhost/website/the-article-description which does not exist.
Can anyone help me out with this?
For situations like #3, I have been placing a function there that returns a string to be used as a prefix URL for the production site. So for this part where you have:
mageUrl="~/images/tree.jpg"
adjust it to this:
mageUrl="<%= [YourNameSpace].AppMethod.IsProd() %>/images/tree.jpg"
'AppMethod' is some static class and the IsProd() function returns the prefix necessary to specify the production path if the app is running in production. Otherwise the prefix "~" if the app is running in dev. You can determine if the app is running in production by reading the machine name.
do you have the RouteExistingFiles property set to true?