How to add the Project root to the localhost path - asp.net

My asp.net project, currently runs from http://localhost:51143/default.aspx
Is there anyway, I could include the root to this like http://localhost:51143/TOrders/default.aspx The reason I want to do this is because the URls that get called on the menu click events refer to "TOrders/Reports/aaa.aspx and so on and in production it would refer to intranet/TOrders/Reports/aaa.aspx and so on.

One solution could be to intercept every request in Application_BeginRequest method in Global.asax.
There you can create a rule to remove TOrders/ from the beginning. Try something like this:
void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.RawUrl.StartsWith("TOrders/"))
Server.Transfer(Request.RawUrl.Substring(8));
}
This may not the nicest solution, but it should be enough to give you idea what to do.
Edit:
Since you have web project (not a web site), you can set virtual path of your project. I think this is what you are looking for.
If you are using VS2010, in Solution explorer right click on your project and chose Properties, then on Web tab, in Servers section, change virtual path of your project to /TOrders/ instead of / which is default value. Now you should get http://localhost:51143/TOrders/default.aspx

You need HttpRequest.ApplicationPath property
or HostingEnvironment.ApplicationVirtualPath property.
Also useful for building virtual paths is VirtualPathUtility class.
EDIT:
Try to copy your project into a subfolder of you web root, for eg. in IIS:
C:\inetpub\wwwroot\ (or what you set)
C:\inetpub\wwwroot\MyProject\
Now in IIS Managment Console, in your Default Web, you create a new 'Application'. Either you upgrade the existing folder or you create a virtual new one to your folder.
Then select a virtual path (TOrders) and set the physical path. There you will also set the AppPool if you have .NET 4, or select the runtime if you have 2.0/3.5 on IIS6.x. Try the highest version first...
Go to localhost/TOrders/

Related

ASP.NET Website Administrator Tool in VS 2013

Is there a short cut method to open website administrator in visual studio 2013, other than the method specified below
http://blogs.msdn.com/b/webdev/archive/2013/08/19/asp-net-web-configuration-tool-missing-in-visual-studio-2013.aspx
By Windows Explorer, copy folder ASP.NETWebAdminFiles and all its content to your solution folder (root folder of your WebApplications).
ASP.NETWebAdminFiles exists in %systemroot%\Microsoft.NET\Framework\v4.0.30319\
%systemroot% usually refers to C:\Windows
On VS2013+ \ Solution Explorer Window, do right click on your solution name; go over Add, on expanded menu click on Existing Web Site... item.
On opened dialog, on left pane choose File System, on right pane browse to your solution folder and select ASP.NETWebAdminFiles then click on Open button.
In added web site, in folder App_Code, find and open WebAdminPage.cs then:
4.1. find class WebAdminPage , find method OpenWebConfiguration that has 3 parameters, replace last line of code with this:
return WebConfigurationManager.OpenMappedWebConfiguration(fileMap, path, "localhost");
you can use domain name or IP Address instead of localhost
4.2. find class WebAdminModule, find method SetSessionVariables, find first if block:
if (application.Context.Request != null) { ... }
inside if block, replace two lines of codes with these:
queryStringAppPath = "/";
queryStringApplicationPhysicalPath = #"D:\PHYSICAL\PATH\OF\YOUR\MAIN\WEB\APPLICATION\";
4.3. Make sure provided physical path ends with a BACKSLASH.
4.4. [NEW] if you going to run this tool on localhost, in class WebAdminModule, find method OnEnter then find first if block:
if (!application.Context.Request.IsLocal) {...}
make whole of block as commented text:
//if (!application.Context.Request.IsLocal) {...}
4.5. On VS2013+ \ Solution Explorer Window, click on ASP.NETWebAdminFiles, on Properties Window set Windows Authentication as Enabled then set Anonymous Authentication as Disabled.
4.6. Set ASP.NETWebAdminFiles website as StartUp Project then run it.
It works, I use it for my applications over Intranet and web.
Good luck.
After a long wait, here is the replacement for the ASP.NET Website Administrator Tool:
Thinktecture.IdentityManager as a replacement for the ASP.NET WebSite Administration tool
Since then, ASP.NET has introduced ASP.NET Identity and community member Brock Allen created IdentityReboot with some significant improvements and extensions. Brock Allen and Dominick Baier have gone even further and created Thinktecture IdentityManager. It's the beginnings of a nice bootstrapped replacement for the missing ASP.NET Website Administration Tool. It is nicely factored and supports both ASP.NET Identity and their alternative called MembershipReboot.
Hope this helps.

Adding Cache headers via a Web.config for specific locations only

We have an application that has been developed by the third party, and I don't want to go back to them to get them to add in cache control for specific pages.
All the pages that need caching disabled are in a single directory.
The issue is that IE seems to not follow Cache-control:nocache properly, so we need to add in Pragma:nocache and cache age as well.
Is there a way to do this using configs in the directory? will it cascade through all child directories? Can it be done via the main web.config?
To be clear, I'm not looking for a way to do this via code, it needs to be via configuration of either IIS or the web.config files.
We're using ASP.NET 2.0 and 4.0, on IIS 6.0.
This can be done in IIS using the UI, it's actually quite easy, or atleast it was in my use case.
All you do is simply open up IIS manager, navigate to the site and then the directory you want to add the headers to Right Click -> properties.
Click the "Headers" tab, and add in the headers you require.
This goes recursively down the child directories, and adds the headers before any added by the code.
In IIS 7.0/7.5, you can use the StaticContent section of a web.config in each of the directories.
You can do that on global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string cTheFile = HttpContext.Current.Request.Path;
if (cTheFile.Contains("/ExtraDir/"))
{
// add your header here
app.Response.AppendHeader("Pragma", "no-cache");
}
//... rest code of...
}

ASP.NET MVC - How to make it work with IIS6

I am having some issues with deploying my MVC 2 application on a IIS 6 server.
I have the following project structure:
/
App/
Controllers/
Helpers/
Infrastructure/
Models/
Views/
Public/ # This folder contains CSS and JS files
Global.asax
Web.config
I have a custom System.Web.Mvc.WebFormViewEngine that tells my application to lookup the views in /App/Views instead of the default /Views.
It works fine on Cassini and IIS 7.5.
I need to deploy my application in a virtual directory on IIS 6 and I am getting 404 errors when trying to access any of my controllers.
I read that I needed to add a Default.aspx with the following code behind:
protected void Page_Load( object sender, EventArgs e ) {
HttpContext.Current.RewritePath( Request.ApplicationPath, false );
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest( HttpContext.Current );
}
It actually called my default controller, and showed the corresponding view, but it's the only page I've been able to get so far.
I tried to enable the wildcard mapping, it didn't change anything. But I'm using ASP.NET 4.0, and it enables routing of extension-less URLs.
I'm not really sure what to do now, I'm not finding any other helpful sources of information on the Internet.
How could I make it work?
See this walkthrough by Phil Haack.
Can't comment yet, but that walkthrough is it.
I did wildcard myself.
It was a while ago, so I don't remember the damn details of what I had to do to get it fixed now, but it took me a few hours.
I was missing some really small detail in his instructions, if I remember correctly. What error/incorrect behavior are you getting? You might trigger my memory.

Read a web.config file in a virtual directory

I have an ASP.NET application (Root Application) that has a virtual directory set up to another ASP.NET application (Virtual Application). How can I make the Virtual Application read values from the Root Application's web.config file? I was looking at the WebConfigurationManager.OpenWebConfiguration() class, but I'm unsure how how to tell it to go up one level from the root. For example, I would tell it to go to ~/web.config to get the the Virtual Application's web.config file, but I need it to go up one more level to the Root Application's file structure. Is this even the correct approach?
Any help would be greatly appreciated!!
You can use the ExeConfigurationFileMap class with ConfigurationManager, like:
string configFile = new FileInfo(Server.MapPath("/Web.config")).Directory.Parent.FullName + "\\Web.config";
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = configFile;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
Response.Write(config.AppSettings.Settings["Test"].Value);
IIS does have programmatic access to its configuration data (which is documented on MSDN and/or Technet). This will be the only supported route (i.e. will continue to work across IIS versions).
Otherwise you can hack a solution (both of these will require higher than usual rights for the process):
Parse the output from appcmd.exe:
E.g. here:
> C:\Windows\system32\inetsrv\appcmd.exe list vdir
VDIR "Default Web Site/" (physicalPath:E:\Dev\weblocal\XYZ)
VDIR "Default Web Site/DevRoot/TestWebClient" (physicalPath:E:\Dev\Tests\ClientSideWeb)
VDIR "Default Web Site/Home" (physicalPath:E:\Data\Homepages)
Read the configuration directly from C:\Windows\System32\inetsrv\config.
I think you will find that your desired behavior is in fact the default behavior.
web.config settings cascade down. Your app will look up to the next hierarchical web.config if it can't find the value.
This would allow you to just look up via AppSettings etc. for most cases.
I'm not sure what happens if you really need to direct access to the file as opposed to the various config api access methods.
I have a setup like this right now using IIS7 with multiple virtual apps configured.
I tried HectorMac's suggestion again and it still doesn't work for me. As a result I am going to seek an alternative to storing my value in the web.config file.

Custom Errors for "App" folders? (ASP.NET)

Where can I setup custom errors for directories in my application such as App_Code, App_Browsers, etc.? I already have customErrors configured in the web.config and that works as expected. For example,
http://www.mysite.com/bla.aspx > redirects to 404 page
but
http://www.mysite.com/App_Code/ > displays "The system cannot find the file specified."
There's no physical App_Code directory for my site. Is this something that I can change in IIS?
You are trying to server content from an Protected Folder... ??
I think you might need to allow access to these folders to get the nice errors you are looking for...
http://www.webdavsystem.com/server/documentation/hosting_iis_asp_net/protected_folders
That being said... there is a reason these folders are protected.
I would never put anything i needed IIS to serve in protected folders.
But there are always reasons to do do something? i have broke a few rules in my short lifespan :)
UPDATE:
Found this when i tried this locally: http://support.microsoft.com/kb/942047/
Looks like those reserved directories throw special 404's you might be able to get IIS to Target the 404.8 type... with out opening up serving to those directories
I believe you will need to set the error pages in IIS itself, as the requests you talk about never reach the ASP.NET application. The reason your first example works is because IIS recognises the .ASPX extension and forwards it to ASP.NET.
One way is to provide a redirect in the global.asax file:
void Application_Error(object sender, EventArgs e)
{
//uncomment this to narrow down 'helpful' microsoft messages
//HttpRequest request = ((HttpApplication)sender).Context.Request;
Exception ex = Server.GetLastError();
//ErrorManager is a custom error handling module
ErrorManager.ProcessError(ex);
Response.Redirect("~/error.aspx?error=" + HttpUtility.UrlEncode(ex.Message), true);
}
{ On a side note, I was getting an exception that I just couldn't track down - it just said 'file not found' but didn't say which file was missing. It turned out to be a broken image reference in a css file - breaking on line two of the code above helped identify the missing file }
Add a Wildcard Mapping to IIS to run ALL Requests through ASP.net, then you can use Global.asax to handle the error.
Taken from here:
Follow these steps to create a wildcard script map with IIS 6.0:
Right-click a website and select Properties
Select the Home Directory tab
Click the Configuration button
Select the Mappings tab
Click the Insert button (see Figure 4)
Paste the path to the aspnet_isapi.dll into the Executable field (you can copy this path from the script map for .aspx files)
Uncheck the checkbox labeled Verify that file exists
Click the OK button

Resources