MVC :: Custom Exceptions - Overriding OnException?
Jun 18, 2010
I've created a custom exception that inherits from Exception. I initially tried using the HandleError attribute with it, but too many things to list went wrong with that. I read about overriding OnException in a base Controller class, and decided to give that a try. This is the code:
[Code]....
This works, but I have a few questions. First, does an error page have to derrive from 'System.Web.Mvc.HandleErrorInfo'? I added a few new properties to my exception, and thought I'd be able to send it as a model to the error View where I could display those properties as usual when populating a View, but when I changed the type that the error View derrives from I get the yellow screen of death.
Also, I read that the above code would cause MVC to look first in the Views folder that correspondss to the controller that threw the exception. This was not the case. I placed the Error.aspx file in that Views folder and was treated to the YSOD again. I simply cut and pasted the file into the base shared folder and it worked. I am using Areas, and wonder if that could be a bug.
The last question I have is about the exception object as it's thrown. After throwing an exception, in the OnException method I thought I'd be able to see and manipulate the exception, but if that is possible I don't know how. What I would really like to do is to treat the exception object like any other object. I want to intercept its handling in the OnException method and send it to a View.
View 1 Replies
Similar Messages:
Feb 18, 2011
I am trying to create a 404 error page and currently I have all of the following/tried all of the following to try and accomplish this. When the user types in :
http://name/something.aspx
It works just as its supposed to. But if the user types in:
http://name/NotAFile
with no .aspx then IIS7 takes matters into its own hands and I get the lovely error page that IIS7 comes with. The goal is that the site redirects with only a 404 status code (so not a 200, or a 302 redirect). I have tried in both the web config with:
<customErrors mode="On" defaultRedirect="~/error/Default.aspx redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/error/NotFound.aspx" />
</customErrors>
This works for the url with a .aspx file extension but not for no extension. Same with this approach in the global.asax
void Application_Error(object sender, EventArgs e)
{
var serverError = Server.GetLastError() as HttpException;
if (serverError != null)
{
if (serverError.GetHttpCode() == 404)
{
Server.ClearError();
Server.Transfer("~/error/NotFound.aspx");
}
Server.Transfer("~/error/Default.aspx");
}
}
The same results are present for this :( My final attempt was to apply this to the web config:
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
With this I just get a plain white screen with nothing on it...
View 1 Replies
Nov 15, 2010
I have some (probably very basic) questions about developing custom controls. I am wanting to derive a set of button controls, and the first one I have started on is a "DeleteButton", that has a additional property (DeleteConfirmationText) that is built into the OnClientClick attribute. To get that added, I overrode the Render method as follows:
[Code]....
This all works well. Now, I will add some logic to make sure that there is some text in the DeleteConfirmationText property, but that isn't the intent of my question.
What I am also wanting to do is to override some of the default property values, so I added that to the default constructor:
[Code]....
Now, in the designer in VS2008, and I look at the properties for the control, there isn't a default value shown in the "Properties" section, and the Causesvalidation still shows the underlying classes default value of "True". As well, in design mode, where the GridView control that containse my DeleteButton control would be displayed, there is an error box "There was an error rendering the control. 'Are you certain you want to delete this attorney?' cound not be set on property 'DeleteConfirmationText'. Here's the markup where I am defining the deletebutton control:
[Code]....
What am I doing wrong?
View 5 Replies
Jan 26, 2010
I'm building the standard 3-tier ASP.NET web application but I'm struggling as to where to do certain things - specifically handling exceptions.
I've tried to have a look around on the web for some examples but can't find any which go as far as a whole project showing how everything links together.
In my data-tier I'm connecting to SQL Server and doing some stuff. I know I need to catch exceptions that could be raised as a result but I'm not sure where to do it.
From what I've read I should be doing it in the UI tier but in that case I'm not sure how to ensure that the connection to the database is closed. Is anyone able to clarify how to do this? Also if anyone knows as to where I could find an example 3-tier web application that follows best practices that would be great too.
View 4 Replies
Jul 19, 2010
I have custom errors set in the web config file as follows:
<customErrors mode="RemoteOnly" defaultRedirect="GenericError.aspx" />
Fine and dandy... I like that mode="RemoteOnly" facilitates development...
For unhandled exceptions, I have in global.asax:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("GenericError.aspx")
End Sub
However, unhandled exceptions are going to the generic error page instead of the informative yellow screen of death preferred by developers. I can comment out the redirect in global.asax, but then I need to remember to change it for the production environment. Is there a way I can check in Application_Error whether I am remote or not to determine whether to redirect?
View 2 Replies
Dec 9, 2010
We have a asp.net application and want to implement logging. The first idea was to use the Application_Error method in the global.asax file.
The problem is that ASP.NET very often seem to throw exceptions internally that are not caused by the application and which seem not to interfer with the users normal workflow. For example we often get HTTPExceptions, UnauthorizedAccessExceptions and others caught in this method, although there is no real error in the application.
View 2 Replies
Jul 7, 2010
I have a class named PageBase inheriting from ASP.NET Page. I want to do something in PageBase's Page_Load.All pages in my application have their logic done on their Page_Load. I'm thinking of a way that Page_Load of both of PageBase and other pages run without having to override Page_Load in pages. Is it possbile?
View 1 Replies
Feb 1, 2011
I have a GridView which uses the Alternatingrowstyle property, but I also would like to higlight each row when the user Edit the row, but using this code, it only highlights the rows that don't have the Alternatingrowstyle.
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.Rows[e.NewEditIndex].BackColor = System.Drawing.Color.Yellow;
gv.EditIndex = e.NewEditIndex;
if (e.NewEditIndex % 2 == 0)
{
gv.Rows[e.NewEditIndex].BackColor = System.Drawing.Color.Yellow;
}
}
View 2 Replies
Dec 7, 2010
I've generated an entity framework class to handle my users from a custom db table. Now I'd like to override the password property of the EF object to Encrypt the value when it is set. From what i understand of MVC this should be done in the model where the EF object lives. I am able to do this easily in the controller but that doesn't seem like the most appropriate place as it will lead to duplicate code anywhere that needs to edit this user class. Is extending the partial class generated from EF possible for generated DB column properties?
View 1 Replies
Feb 12, 2010
I have a GridView control hooked up to a datasource that I allow row editing on. I use the standard edit/save/delete buttons that point to the Update, Cancel, and edit GridView commands.
I would like to add an additional button, "Next" inline with the save/cancel button. This would function the same as the save button, but would also add an additional row to the grid, and place that row into edit mode. To do this, I've added code to the Updated event of the GridView, which adds a new record to the db, rebinds the grid, and sets the editindex. This seems work, but the grid does not actually return in edit mode. I stripped out the new record code and simply left the statement to set the editindex, but the grid still will not stay in edit mode.
Is it possible to force the grid to stay in edit mode in this scenario? It appears that unless you set the cancel property of the GridViewUpdatedEventArgs, the Grid is going to flip to view mode. I don't want to set the cancel property in this case, because I want the GridView to trigger my datasource object to save the record.
If this doesn't work, it appears that I will need to allow the update to occur normally, return to the client, and then send another server request to add the record. I'd prefer not to do it this way, I would like to perform the necessary operations in context of one roundtrip.
Here's what the code (simplified) looks like:
protected void Button1_Click(object sender, EventArgs e)
{
// Works Fine
this.sampleDataSource.Insert();
this.sampleGridView.DataBind(); [code]...
View 3 Replies
Jan 25, 2011
I would like to provide my client with a aspx pages, with code behind provided, etc. But in some cases they will want to write their own markup - I'm thinking of having 2 directories [siblings of each other], one for the pages I provide, and the other for their custom markup. At page serve time, check to see if the client wrote their own markup and if so use it. Otherwise use the default provided.
In a nutshell I would like for my clients to be able to modify the pages I provide, but if they blow it up have a very easy way to back it out. Or to put it in a more positive light, allow them to override my pages easily.
View 5 Replies
Feb 27, 2011
As the server of my website is located in a different timezone, is there a easy way to override the server local time by a setting in either web.config or global.asa?I could of course just use .AddHours() in all my .now() statements, but as this problem occured recently when I moved the site and that the .Now() syntax is used a number of times in the application I was looking for a another solution than changing my code everywhere I've used this syntax.
View 2 Replies
Feb 13, 2011
I'm new to MVC and I have a question and I need your inputs.I have this portion of code inside a method (let me just call this MyMethod):
[Code]....
where I am simply using the default CreateUser method from default Membership class to create a new user. This method simply adds the user on the default membership table. Now I have another table which I call userDetails table and I am hoping to add additional information related to the user recently added on the membership table.I can simply add more codes inside MyMethod to do the above logic of adding the additional information to another table. However I was thinking that if I do this... and I delete the user using the ASP.Net Web Application Administration Tool, I may end up with orphan entry on my userDetails table after the user is deleted from the membership table (since the administration tool wouldn't know MyMethod and use the default method from Membership class).So is there a way I can override the CreateUser method from the System.Web.Security.Membership class such that it goes through this flow?
1. invoke the original or base CreateUser method (the user is now created during this step)
2. determine user_id of the newly-added user on membership table.
3. add more user-related information on another table example: userDetails table.
This way, every time I call the CreateUser method it automatically adds the necessary entries on the membership and userDetails tables.If I create my own membership class inheriting from the original class, it would mean a lot work rewriting lots of methods when in reality what I'm trying to achieve is only adding additional information related to the user. I tried modifying the default membership table from the provider database but I get and error during the user-creation process. This gives me the impression that I am not allowed to modify the existing tables... which would lead me to creating a custom provider.
View 2 Replies
Dec 31, 2010
explain the diference between these Page_Init() and OnInit() events and when we should use which one?
View 2 Replies
Oct 21, 2010
Is it possible to override the default 'There is no Data available' message in MVCContrib Grid with a custom message?
View 1 Replies
May 27, 2010
Is there built in methods into .net framework to do the following:
Get role GUID from user name/user GUID
Get role name from role GUID
So far I have been sending queries to the asp_roles and asp_users tables to obtain that information and I'm wondering if there is a nicer way of doing this?
I have the following methods that I store in the ASPUtilities class:
getRoleGUID(guid userGuid) { LINQ joins }
getRoleGuid(string userName) { LINQ joins }
getRoleName(guid roleGuid) { LINQ joins }
EDIT:
I have just looked into extending SQLMembershipProvider examples.
Few examples completely override the SQLMembershipProvider, but I think what I'm interested is just adding few extra methods to deal with the roles by using LINQ. Is this feasible?
View 1 Replies
Jan 3, 2011
[Code]....
The model has a required attribute for the first name property:but I would like to make this happen automatically if the property has the CustomRequiredAttribute. Is there a way to override the Html.TextBoxFor() and check for the attribute on the given property? Or is there another way to accomplish this behaviour?
View 3 Replies
Jan 21, 2010
How should I log exceptions? I never tried logging in .NET before. Nor try to dump exceptions to a txt (or binary) file. I dont require a text file, just a way to view the logs with the file and line #.
-edit- using asp.net
View 13 Replies
Aug 5, 2010
I have the following code
[code]....
and while that presents a friendlier error message to a user, I've forgotten how to show me the "real" error.
View 2 Replies
Apr 1, 2011
I have a set of WCF services which I have been using with an ASP.NET MVC application so far. These service operations return a FaultException when the server has identified problem with what the client has submitted. For example:
[code]...
However with Silverlight this all fails. The server returns a 500 status code with the faultexception (as expected) but to Silverlight this just looks like a duff response.
The following MS article indicates a (ugly) work around for this: [URL] This workaround makes the service transmit 200 status codes, even if there is a FaultException, so that the Silverlight client can get them. But this will mess up 'normal' clients of my service (my ASP.NET application, other users in the wild).
However, the point of services is to have seperation from your clients. I still want my services to return 500 status codes so that my ASP.NET application can detect the FaultExceptions and handle them. But I also want Silverlight to be able to handle them too.
View 1 Replies
Mar 11, 2010
string lNewHTML = Regex.Replace(lOldHTML, "(word1|word2|word3)", "<a href="page.aspx#$1">$1</a>", RegexOptions.IgnoreCase);
The code works, but I need to include some exceptions to the replace - e.g. I will not replace anything i an img-, li- and a-tag (including link-text and attributes like href and title) but still allow replacements in p-, td- and div-tags.
View 2 Replies
Mar 2, 2011
How can we prevent page crash in asp.net? Is there any generic function or place like global.asax where we specify a file to redirect to when an unhanded exception occurs? (like we redirect to a specified page when 404 page not found exception occurs?
View 3 Replies
Feb 11, 2011
I need to handle 404 exceptions differently than all other types of them. What is the best way to identify those 404 exceptions (distinguish them from other exceptions)?
The problem is that there is no a special exception class for 404 errors, I get regular System.Web.HttpException with Message = "File does not exist."
Should I just use exception's message for it or is there a better way?
View 4 Replies
Apr 4, 2011
I have a datasource that uses a business logic object for the select event. How can I catch an exception that occurs in the business logic layer and pass it to my presentation layer to display to the user in a label?
View 1 Replies
Aug 3, 2010
There used to be an article at ELMAHs docs site that contained this information, but it seems to have been removed. I think it's as simple as setting a configuration setting in the section, but I haven't a clue what it isCan anyone take a few seconds and drop the configuration setting here?
View 1 Replies