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

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.

Related

Separate MVC application as a folder using Handler Mapping

We have a legacy webforms app and a new section of our site developed with MVC. It works a treat as an application in the Default Web Site in IIS.
Only downside is that the root site's session object isn't passed to the application. We have a work around for this.
A consultant is convinced that we don't need to set it up as an application and it can all be done via handlers and route mappings. He conveniently has not provided us with any details though and I have been tasked to implement it.
The best I have achieved was to add a Module Mapping to the Handler Mapping section in IIS. This filters coolmvcapp/* with Module isapimodule to a virtual directory who's physical path points to our new MVC app. The result is an empty page with a 200 response. Most other combinations I have tried resulted in a variety of errors. (Turns out an empty page is returned without adding the handler mapping - hey ho)
So, is this approach even possible?
I am aware that MVC and webforms can be merged together but for reasons this is something that we want to avoid.

issue with child application paths

I created a new website in IIS called wbsite1. Then I added an application to website1 wich is a website called website2. The problem appeared when we tried to access links in website2. The browser always refers to the root url and redirect so we were trying to change all the urls in website2. For example, if we have a url in website2 like :
<a href='http://localhost:2030/website2/cms/default.aspx...'
and we click on that link the browser redirect us to :
http://localhost:2030/website1/default.aspx
Why am I getting this behavior? And what is the best workaround to integrate two websites without trying to mess with every link in the website? Thank you very much.
We are facing troubles when I try to access a page in the child application it redirects to the default page of the main application and gives the error:
Session state is not available in this context.
I found the solution for my question and it's this :
unfortunately i have to "rewrite" all our links in my code.
so i created a function which will automatically add prefix to external links,
and store base link for other site it configuration.
I did such project with success. thank u for all your replies
i found another solution also and it's using a web proxy but i can't use it in my scenario

Can't execute file via Server.Execute()

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.

Getting text after URL in asp.net / URL Rewriting (sort of!)

My app is a very simple "one page" type app-
It has Default.aspx
I'm basically trying to get, for example:
www.myappurl.com/this is my text
I want to get hold of "this is my text" from the above example.
This will be displayed on the page (for now)
I didn't really want to have to use any complext url rewriting things for this...
(My hosting provider uses IIS6)
I tried using a 404 handler, but this is a bit long winded, and i'm using shared hosting, that can't set the "execute url" on custom 404 pages.
Any other ideas?
You can add a mapping for all requests with the * extension to the ASP.NET isapi dll (GET/POST) verbs. You will need to uncheck the "verify file is on disk" checkbox when mapping the extension in IIS. (In IIS7 integrated mode, you map the extension in the web.config as well). Note that this will caause everything to be served by asp.net, even images and script files, which can slow things down.
Then create a handler mapping in your web.config to a http handler you create.
From there, in the ProcessRequest() method of the handler, you have access to the HttpContext that spawned the request and can manipulate the URL from there.
That is the easiest option, you could also create a HttpModule, or have the default page at root redirect to http://www.domain.com/default.aspx/this is my text, in the code-behind of default.aspx, you will be able to get the text following the page and slash.

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