URL routing relative paths with webforms ASP.net 4 and IIS6 - c#

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?

Related

ASP.Net Core Returning 404 when image url spaces encoded as +

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_

why do not show alias name in iis?

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");

Directory in MVC3 Project

I have a "Tools" area of the MVC3 site I'm currently working on. One of the tools I'm integrating on the site I need to run in a virtual directory. Setting up a virtual directory under the /Tools folder works fine for the app itself, but for navigation to /Tools/, I'm getting "Directory listing not allowed". How do I tell IIS to let MVC routes handle this URL?
We'll be using IIS6 in production, so it's important for it to work with that.
Thanks in advance.
EDIT: For clarity, here's the setup:
/Tools/RoutedTool1
/Tools/ToolInVirtualDirectory
/Tools/RoutedTool2
/Tools/
The routes for the routed tools work fine, but since I had to create a directory under the root to setup the "ToolInVirtualDirectory", IIS is hijacking the "/Tools/" request and trying to send it to the directory, ignoring the route.
You should set routes.RouteExistingFiles = true in Global.asax.cs.
Remember that since this property plays at global level you have to ignore the css files and other stuff you don't need to handle by the routing infrstructure before setting this. For more idea please refer this post.
In your RegisterRoutes section in Global.asax, you add ignore routes that the routing system ignores. There should already be an example for .axd's in the config.

Removing Localhost url in asp mvc

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!

URL Rewriting in asp.net c#

I'm working on a real estate website. It would be ideal to have my client's featured properties have their own unique URL like:
www.realestatewebsite.com/featured/123-fake-st/
I'm constructing a CMS for my client so that they can add/delete featured properties in an admin backend, meaning that I need to write a program to automatically add the new URL for them based on the address they input in the database through the CMS.
I'm new to URL Rewrite. What would be the best way to go about this? I've considered using RewriterConfig in the web.config, but then I'm worried I would encounter problems writing a program that adds new rules to the web.config file. I thought about using a regex expression in the RewriterRule to find anything after /featured/ in the URL, but then if I'm just using the address in the LookFor then how would it know which property ID to use in the SendTo?
It would be ideal if I could just have a file put the address after "/featured/" into a string, look in the database for the address and retrieve the Property ID and then redirect the users that way.
As I said, I'm new to URL Rewriting and it would be great if someone could point me in the right direction.
Thanks!
-Aaron
There are different ways of doing this. Common to all solutions are the following:
Set up a algorithm to create the URIs and store them in the database (changing space to - is a simple way to achieve this.
Route the URI by making the address string into a parameter
Routing can be done a variety of ways.
If you have control of the server, or they have control of the server, you have the ability to set up IIS rewriting on the IIS instance on their server (good starter URI).
If this is hosted on an ISP, you may not have this option and have to use IIS rewriting and will have to use ASP.NET routing. Here is a good article to start with to undestand this. If you are using MVC, the routing is "built in".
I would suggest using URL Rewrite Module for IIS7, look here:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Categories