Can't execute file via Server.Execute() - c#

I'd like to execute a file in a subsite. Both the parent and sub site are web apps. I don't want to redirect to the sub site file. I'm trying to execute the sub file using Server.Execute() but it isn't working.
The main URL is:
http://mysite.com/subsite
Virtual path to file in subsite: ~/subsite/subfile.aspx
When I access http://mainsite.com/subsite/subfile.aspx, it works fine. When I do
Server.Execute("~/subsite/subfile.aspx")
from the parent site, I get:
Could not load type 'Subsite.SubFile'.
Any ideas what I'm doing wrong?

I'm pretty sure server.execute doesn't cross application boundaries. I'm not 100% certain of that; but given the error message it seems likely.
Basically the error is saying that it can't locate (or load) the assembly that the Subsite.SubFile class is defined in.
I can think of two possible solutions.
Copy the assemblies for the Subsite into the main sites bin folder. This might work because the main site could then locate the assembly and execute the code. There are obvious drawbacks here. Such as needing to have all of the config information from the subsite copied into the main sites web.config. Also, any assemblies with the same name would be problematic; and, of course, needing to deploy the subsite on top of the main site every time there is an update...
Instead of doing a server.execute; use the HttpRequest class to make a regular http call to the sub site and then parse the response and write back to the browser what you need.

Related

Using multiple global error files in the same solution (Web Application)

I have an Web application with 3 sub folders that I want to use in the solution:
HR
Business
Money
Each has a sub application that uses common parts of the solution like Master Page, CSS, etc.
Each application has its own error page in their respective folders.
But there is only 1 Global.asax at the top folder.
I want to handle error at global level for each application so that the unknown errors always land up at the correct error page for each solution when we are browsing their pages.
Eg.
1. Error in /HR/Page1.aspx goes to /HR/ErrorPageForHR.aspx
2. Error in /Business/Page1.aspx goes to /Business/ErrorPageForBusiness.aspx
3. Error in /Money/Page1.aspx goes to /Money/ErrorPageForMoney.aspx
How can I achieve this ?
Can I make multiple Global.asax in each folder or some other way?
A viable approach is to have the logic of your error handler into a Class Library with a Class to handle the error accordingly. This project could also be useful for ther common behaviors shared across the 3 web applications. Then Add that library as reference to each web application, and within each Global.asax file, on the backend, include the Class Library project main namespace and call your ErrorHandler Class logic. That is what I would do...
You cannot have multiple Global.asax, unless you convert folders to Areas which is probebly overkill.
You can get request with HttpContext.Current.Request and redirect user to proper error page based in URL.
Additionally, you can put different web.config file in each folder with customeErrors for 404, 403.

URL Rewriting not working with URL ending ".ad" or ".cd"

I'm having some difficulties with my custom URL-rewriting setup. I'm not using any 3rd party tool to manage this, just using the global.asax by looking at every request and processing it.
The way I am using it is like:
www.mydomain.com/site/google.com
This page contains information about the site "google.com". I'm using the actual domain within the URL. This is all works fine.
I have simplified the code that I'm using just to show you an example of how it works:
Dim myContext As HttpContext = HttpContext.Current
Dim URL As String = myContext.Request.ServerVariables("URL")
If URL.ToLower.Contains("/site/") Then
URL = URL.Trim("/")
Dim strURL As String = URL.ToLower.Split("/")(1)
Redirect301("/site.aspx?url=" & strURL)
Exit Sub
End If
The issue I'm having is for certain domain extensions, the page will just load up custom 404 Not Found and I have no idea what the cause is.
Example of pages that don't load:
/site/google.ad
/site/google.cd
I'm guessing that the system thinks that .cd and .ad files are actual physical files and when it doesn't find them, it shows the custom 404 error. It doesn't actually looks like the request is getting through to the global.asax. When working in local environment, they actually load fine but only on the live server it has this issue and this is why it has been a nightmare trying to figure it out.
Another issue I found was loading the following URL:
/site/prn.com
This shows a 404 error again but this time not the custom one I created but the actual hard looking .net 404 error page. This also works fine in local environment.
There must be some IIS setting or code change I could do to try to get this resolved.
Thank you for your time :)
Aki
Check this link . Error message when you try to browse a Web page that is hosted on IIS 7.0: "HTTP Error 404.7 – FILE_EXTENSION_DENIED
This is the reason .ad .cd extensions are blocked.

How to retrieve on which site the web application is currently running?

I am currently running a WCF service on an AppFabric server and my application needs to load a the web.config file dynamically to retrieve custom configuration sections.
On my development machine I can just load the configuration like this:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath);
But on the test machine (AppFabric server) I am getting an exception and it seems that I need to specify a third parameter which is actually the site the web application is running on:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath, "MySite");
So I tried to hard code it and it worked. Anyway this is not acceptable, so I need to dynamically provide the site to the WebConfigurationManager because I do not on which site the service will be running in the future. Do anybody knows how to achieve that?
Thanks.
If you are running this code as part of handling a request you could use:
Request.ServerVariables("server_name")
see: http://msdn.microsoft.com/en-us/library/ms525396(VS.90).aspx
Edit based on your comment
The parameter that you need is the Site Name, not the machine name, your code be running on many machines. If the code is running somewhere where it no longer knows that it is on a web site, then it is difficult for it to get the name of the web site that it is running on.
You then have two options:
Send the name as a parameter from a layer that has httpconext
Not sure if this will work: but you could try adding a reference to system.web to your project. It may compile, but you could get a null reference exception when you run it. Probably worth a try.
How about Server.MachineName

Reference file outside of Web site directory

How do I reference a file outside my web site's root directory?
For example my website is located at C:\dev\TestSite
I am using ASP.NET with XSP. The webapp will be deployed on Apache using mod_mono.
I have images in C:\images and I would like to do this:
<img src="C:\images\logo.gif"/>
Your img tag's src value is going to be sent to the client. You need to specify those paths relative to your document root. Your best bet is to set up a virtual folder (in IIS, alias is the apache equivalent) to point to the c:\images path and then change the mentioned tag src path as follows
<img src="/images/logo.gif" />
To do this in apache, you need an alias in your httpd.conf. The line looks like this
Alias /images c:/images
Here's the docs http://httpd.apache.org/docs/2.0/mod/mod_alias.html#alias
While it might be possible to do this through some hacky method, it's not a good idea. Allowing the IIS account to access files/folders on the greater file system would be a big potential security hole.
The best way to accomplish this is to use IIS Virtual Directories. Put your content in folders dedicated to supporting the site, DO NOT make your entire C: drive a virtual directory.
I'm going to assume that C:\images\logo.gif is the path on the server, and not the path on the client.
The src attribute is interpreted by the html client (i.e. Internet Explorer). The client can't see anything outside of your web directory. In fact, the client can only see things inside your web directory if you've provided permission for them to do so. Thus, this isn't an ASP.NET issue, but an issue of how web clients have access to web servers... which is designed this way for security.
In order for your application to use these images, you've got a couple of options that immediately spring to mind - neither of which is ideal:
The ASP.NET code (in the codebehind) is going to need to go and grab the file, and serve it out in the html stream that is being served to the client, which is more a complex task than I suspect you are willing to embark on.
The ASP.NET code (using System.IO) is going to need to go and grab the file from it's home location in C:\images\logo.gif and copy it to a location that is accessible to the client - you could create a temporary directory, copy your image to it, serve it out, delete it, delete the directory.
Both of these are certainly hacks that should be avoided if possible, but if you're adamant that this is what you want to do, this will allow you to do it via your ASP.NET app.
The most ideal solution is to add C:\Images as a virtual directory to your document root, i.e. /ImageCentral - this way you can have images that are central to multiple websites stored in this directory, it can then be referenced by clients for any of the websites just by adding virtual directories to each of them pointing at the central images folder. As DaveSwersky points out, don't make any directories containing sensitive information virtual directories, the minute you add a virtual directory to an externally visible website, you're giving people free reign to any of the information in it.
Good luck
You can't do this from within the HTML code of your page. The HTML page can only reference web accessible content (in regards to images, CSS, javascript, etc). You could create a virtual directory that points to your images folder so that it becomes web accessible.
EDIT:
The apache way inside of your conf file.
Alias /images "C:/Images"
And a little walkthrough from some dude.

Running an MVC Application as a Sub-Application?

I'm attempting to create an MVC application as a sub-application to my standard Asp.Net Web application. Both of these projects are inside the same solution. While the parent application appears to be going fine, I'm having trouble getting the sub-application to work. After some massaging of my two web.configs, I was able to get the Asp.Net runtime to accept the configurations, but I have been unable to browse to any of the pages/controllers in the MVC application, including the root of the sub-application ("http://RootSite/SubApplicationName/"). I continually get 404's.
Actually, I do get a response when going to the url "http://RootSite/SubApplicationName/Home/Index/". It redirects me to index.aspx in that folder, and throws this error:
The view 'Index' or its master could not be found. The following locations
were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
The sub-application in IIS (7) is set up fairly straight forward: it's set to run in the same application pool as the parent app, which runs Asp.Net 2.0 in integration mode.
My suspicion is that I have something in the web.configs that is throwing it off. Are there things regarding, say, HTTPModules or URL authorization modules, etc., that I should confirm aren't getting in the way of MVC?
Also, in the global.asax.cs file, should the default route be different? By default, the url parameter passed to routes.MapRoute is:
"{controller}/{action}/{id}"
Should it be preceded by the name of the sub-application, like so?
"SubApplicationName/{controller}/{action}/{id}"
I attempted a change like that, but it did not fix things.
Any ideas are much appreciated. Also, general information about setting up an MVC web application as a sub-application would be great.
Thanks.
I did something similar, however not the same, I had to load views from a separate dll. In my case it was a class library, not a different web app, but it should work the same as far as I know.
The first thing you have to do is to create a VirtualPath Provider to tell the routing engine how to look for your stuff in the subapplication views. A great explanation of how to do this can be found here:
http://www.wynia.org/wordpress/2008/12/05/aspnet-mvc-plugins/
I'm sure that will get you started ;)
Make sure that you haven't made any spelling mistakes in the names of your Views directories. I was receiving the same error message and after 30 mins of head scratching realized that I had misspelled the folder name for one of my Views. The IDE did not pick this up in any meaningful way (i.e. it would have been nice if it explicitly told me that the path to the view that I was referencing was not correct -- "not found" could mean a few different things).
Sub application doesn't suite to MVC web application directly. you have to write a lot of hacked code in global.asax. Use sub domain rather than sub application.

Categories

Resources