Run Every ContentResult In A Controller Through A Universal Try / Catch Elegantly?

Feb 28, 2011

I am assuming there is an elegant way to do this, but I do not know what it is. In an application I am working on, I have a lot of ContentResults that return xml. Every action that does return xml runs through the exact same try/catch block and I am repeating myself over and over again - here is an example of what I am talking about:

public ContentResult SomeAction()
{
try
{
//some stuff here
}
catch(Exception ex)
{
HandleErrorMethod(ex);
}
return this.Content(someObject.ToXmlString(), contentReturnType);
}

This happens 3-4 times in a controller so I figure there is a way I can either paint it with an attribute, or run some sort of method in the global.asax or something like that to prevent repeating myself over and over - not to mention code changes in the future.

View 2 Replies


Similar Messages:

.net Mvc 3 Dataannotation Can Use In Different Class Scenario For Edit And Add Elegantly

Mar 14, 2011

[Code]....

How can I maintain using data annotation attributes for validations in mvc3 different requirement for edit and create in an elegant manner. I have an different edit and create requirement.

View 2 Replies

MVC Google Sitemap From ContentResult - Incorrect Namespace

Feb 15, 2010

I created a sitemap generated by a ContentResult however Google keeps telling me my namespace is incorrect. Any ideas?

My Code:public ContentResult Index()
{
//Build RSS for sitemap
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
const string url = "http://www.openarmssoberliving.com/{0}"; [code]...

View 1 Replies

DataSource Controls :: Catch Don't Catch Any Error From SQL 2000 Transition Store Procedure

Feb 7, 2010

Here is the code copied from [URL] In asp.net code behind, I use try-catch try to catch any error but never catch it. In SQL database, if I rename Employees to Employeesx or change column DepartmentID to DepartmentIDx, record will not be deleted (it is right) without any error (it is wrong, suppose catch an error).

CREATE PROCEDURE DeleteDepartment
(
@DepartmentID int
)
AS
BEGIN TRANSACTION
DELETE FROM Employees
WHERE DepartmentID = @DepartmentID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RAISERROR ('Error', 16, 1)
RETURN
END
DELETE FROM Departments
WHERE DepartmentID = @DepartmentID
IF @@ERROR <> 0
BEGIN
ROLLBACK
RAISERROR ('Error', 16, 1)
RETURN
END
OMMIT

View 2 Replies

.NET Universal Login From Multiple Sites?

Sep 17, 2010

I work for a company that has many sites. Each site requires a login at some point. We have a single Accounts database that all of the sites hit.One of the requirements for the login system is that if we login on one site, we should automatically be logged in elsewhere.

The way it works right now is that on page load (or init, forget which) at any of our sites (let's say site1.com), it redirects to a "master" site (let's say sitemaster.com). On sitemaster.com there is a web service which checks to see if a cookie exists on sitemaster.com for that user. If it does, it redirects back and lets site1.com know that the user has already logged in (site1.com then creates a cookie for site1.com so we don't have to redirect to sitemaster.com in the future). If the user is not logged in at sitemaster.com, site1.com will then ask the user to login and, on submit, it redirects the user to sitemaster.com which logs them in, creates a cookie under sitemaster.com domain, redirects back to site1.com and lets site1.com know that the user is logged in (which then creates the cookie for site1.com).

On logout a similar method is employed.

Is this the best way of handling universal logins, or auto-logins across multiple sites? Is there a better way of doing things?

Some requirements:Most of our sites are in .NET 2.0, but there are plans to update them all to .NET 4.0. We want the best method for this auto-login system so if something requires .NET 4.0, that's ok.

The solution should not require javascript (our current solution requires javascript during the login process).The solution should not require Flash.It's ok to use iframes, as long as javascript is not requred.

We would like to be able to do AJAX logins (using jQuery) without having the site refresh. The current solution, due to the redirects, prevents us from doing that. (eg: Pop up a modal box asking the user to login, and when they do, the modal box disappears and some content is changed, but no site refresh required). This isn't absolutely necessary, but at the very least we should be able to verify a login using AJAX before any redirecting takes place.

I also have some questions related to the best way of re-using the auto-login code across sites (varying from ASP.NET 2.0 Forms to ASP.NET 4.0 MVC, but I'll save that for another question).

View 2 Replies

Web Forms :: Universal Text Filter For Postback?

Mar 3, 2010

I'm creating a small, private social networking site. I wanted to add 'emoticons' by filtering text such as and replacing it with an image tag. That's easy enough with stringname.Replace() but what i'm wondering is, is there a way to add some sort of a 'filter' that will automatically do that replacement for me on ANY string post back so i don't have to add the replace code to every page?

View 4 Replies

Getting Unhandled Exception But Unable To Catch With Try Catch

Sep 14, 2011

I get this error in the browser:

Code:
Thread was being aborted.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Threading.ThreadAbortException: Thread was being aborted.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ThreadAbortException: Thread was being aborted.]
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +486
System.Web.ApplicationStepManager.ResumeSteps(Exception error) +501
System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +123
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +379

Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

I have try{}catch(Exception ex){} in the right place:

Code:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
// all processing occurs inside here
}
catch(Exception ex)
{
}
}

I even created a global.asax file and on the Application_Error event, I wrote a code that would email me the error (and i'm not getting an email regarding that error when I get the error shown above). I know for a fact that the thread is going inside the "try" statement because I send emails to myself whenever it finishes certain codes inside of it. So how come I'm getting that error in my browser instead of it being handled in my "catch" statement? I have two problems here, one, why is the exception not going to my "catch" statement, and two, why am I getting that error in the first place.

Note: my web app calls a webservice.

View 2 Replies

Web Forms :: How To Make Universal Search Option For Website In Home Page

Sep 30, 2012

I have seen in so many sites on the home page there is a search option and after searching any word of the website it displays and in w3schools it navates to goolge with his reference.

How to write a code for the same and which control we should use.

View 1 Replies

C# - Using Grasp Controller With MVC Controller - How To Make An Object Always Visible For A Controller

Dec 28, 2010

UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, that follows the UP(Unified Process). It uses a Grasp Controller pattern to interact with domain classes by some methods like NewSale(), AddNewItemToSale() and CloseSale. In windows form, I can instantiate a object of this class in the UI and then use its methods to perform the actions. This works well in Client apps, but when I use asp.net mvc, I cannot find a way to instantiate an object (one for each user) that was always visible for a Controller (MVC). I cannot insert as an attribute inside Controller because it always create a new one.

View 1 Replies

MVC :: ContentResult Type="text/xml" - IE Cannot Display This Feed?

Oct 30, 2010

When I return a ContentResult that is ContentType="text/xml" I get an error in the browser - Internet Explorer cannot display this feed.What would be in the XML that is triggering IE to think it is a feed, and how do I tell IE to not think such things?When I set Content to contain only the first, 2nd and last line of the XML stream, it renders correctly as XML.Here is my code:

[Code]....

And here is the XML:

[Code]....

View 4 Replies

Forms Data Controls :: How Show Local Time In Datagridview Collumn DateTime From Database With Universal Datetime

Jun 2, 2010

How show local time in datagridview collumn DateTime from database with universal datetime

View 2 Replies

MVC :: Out Of The Box MVC2 Controller / Delete Controller Is Refusing To Return Any Class Information

Nov 6, 2010

my MVC2 delete and only my delete controller is refusing to return any class information. Its really similar to my edit function and the views are all auto-generated so I don't see the problem.

Function Delete(ByVal id As Integer) As ActionResult

View 4 Replies

MVC :: Pass Parameter From Controller To View And Back To Another Controller?

Aug 16, 2010

I have a simple model where a Person has Gifts. I have a view which is a list of Gifts belonging to one Person.

My problem is with the Create action for a new Gift. I want it to default to the PersonID that we are already viewing the list of Gifts for. I tried simply passing the last PersonID (they are all the same)

Html.ActionLink("Create New", "Create", new { id = Model.Last().PersonID }) which works fine if there is already at least one Gift for that person but if this is the first Gift I don't have a value.

My Gift List controller knows the PersonID I want to pass but the view doesn't.

How do I pass this PersonID from my Gift List controller to my Gift Create controller via the Gift List view? Or is there a better way to do this?

View 2 Replies

MVC :: Session Vs TempData / How To Persist Values From Controller To Controller

May 30, 2010

I have a filter on my MVC web site. I display some records in a few different controller actions but when moving from one action to another I want to apply those filter values.

How can I persist values from controller to controller?

Should I use Session? TempData?

I am using Structure Map for IOC.

Maybe I could have a class that contains a Property for each Session Value that I use in my application and inject it on the controllers that need session?

View 10 Replies

MVC :: Get The Current Action / Controller Name In A Controller Or Class?

Mar 14, 2011

How do you get the current action / controller name in a controller or class?

i can't show it in my view but that's not what i want.

View 1 Replies

MVC :: C# Controller Works, VB Controller Doesn't ?

Jul 22, 2010

C#: HomeController.vb: works fine HttpPost RsvpForm gets the object filled with user input.

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using RsvpInC.Models; namespace RsvpInC.Controllers{ public class HomeController : Controller { //
[code]...

View 5 Replies

MVC :: Calling Action Of A Controller From Another Controller?

Jan 24, 2011

I have a User entity and a department entity. I have a UserViewModel and DepartmentListModel which has List of departments.

UserViewModel has a property of type DepartmentListModel .

Now on user/create action I need to populate DepartmentListModel by calling DepartmentController's list action. List action returns populated DepartmentListViewModel.

From UserController how do I set DepartmentListModel ?

I tried doing so

[Code]....

But RedirectToAction returns RediretToRouteResult

View 7 Replies

MVC :: Area Controller To Inherit "Shared" Controller?

Mar 11, 2010

Say I have several Areas that are baed on user roles.Differnet roles have different functionality, but also have some "shared" functionality like say "Payments"Now, I created a PaymentController (and it's views for listing, creating messages) in Shared (not an area) and it works fine.

Would it be possible (and how?) to create a PaymentsController class in one of the Areas, have that inherit the PaymentController Class in Shared?

If I can do this, I can successfully share common functionality across areas, and at the same time extend the derived controller to include some 'area specific' functionality to common shared controllers. (Such as the "REturn to xxxx" text and route of the returnurl, which are a bit more complicated than I just made it sound)

View 3 Replies

How To Use Try ,catch In Asp.net

May 29, 2010

Here i have small doubt,how can i use try and catch.Please give me brief idea.hen ever an error is raised in try block,how to handle it in catch.is it enough to give message for user or shall i handle there it self?

View 7 Replies

MVC :: How To Transfer Control To A Controller's Method From A Controller's Method?

Jan 19, 2011

How can i transfer control to a controller's method from a controller's method..I am having two Controllers Home and User.. After username and password are verified inHome Controllers method , i want to show the User Controllers index page , so i have to callindex method of Home controller..How can i do it.

View 5 Replies

When To Use The Try Catch Condition

Feb 23, 2010

Do I have to use the 'Try Catch' condition or possibly find ways to catch the error myself? Do you use the Try Catch all the time, when, why not?

View 7 Replies

.net - Cn Use Try Catch With An If Statement In Asp.net

Jan 26, 2011

i am having a listbox which contains some entities which i need to select in order to make the submit button work successfully but when no entity is selected it should give an error

protected void SubmitBtn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
on.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
[code]...

View 1 Replies

Significance Of Catch All

Apr 25, 2010

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

What is the significance of pathInfo?

View 3 Replies

.net - How To Catch Particular DB Error In ASP.NET

Jan 29, 2010

In my SP (Sql Server 2005) I'm raising an error usingRaiserror ('No Records Fetched' , 16 ,1)I want to catch this particular error in ASP.NET.. how do I do it ?

View 2 Replies

C# - When To Use And When Not To Use Try Catch Finally

Apr 4, 2011

I am creating asp.net web apps in .net 3.5 and I wanted to know when to use and when not to use Try Catch Finally blocks? In particular, a majority of my try catch's are wrapped around executing stored procs and populating textfields or gridviews? Would you use Try Catch EVERYTIME when you execute a stored proc and populated a data display control?

My code block usually looks like:

protected void AddNewRecord()
{
try
{[code].....

View 7 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved