Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6162729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:42:12+00:00 2026-05-23T21:42:12+00:00

Get User Action [HttpGet] [ActionName(Index)] public ActionResult Get(int id) { User u = UserCore.GetUser(id);

  • 0

Get User Action

[HttpGet]
[ActionName("Index")]
public ActionResult Get(int id)
{
    User u = UserCore.GetUser(id);

    if (u == null)
        return Content("user not found", "text/plain");

    return new XmlResult(u);
}

UserCore.GetUser

public static User GetUser(int UserID)
{
    using (PanamaDataContext db = new PanamaDataContext())
    {
        return (from u in db.Users where u.UserID == UserID select u).FirstOrDefault();
    }
}

The Route

routes.MapRoute(
    "GetUser",
    "user/{id}",
    new { controller = "Users", action = "Index" }
);

And finally the test URLs

/user/9000 returns “user not found” as expected (does not exist currently)

/user/75 (actually exists in the DB) however returns:

Cannot access a disposed object. Object name: ‘DataContext accessed
after Dispose.’.

[ObjectDisposedException: Cannot access a disposed object. Object
name: ‘DataContext accessed after Dispose.’.]
System.Data.Linq.DataContext.GetTable(Type type) +1020550
System.Data.Linq.CommonDataServices.GetDataMemberQuery(MetaDataMember
member, Expression[] keyValues) +120
System.Data.Linq.DeferredSourceFactory1.ExecuteKeyQuery(Object[]
keyValues) +258
System.Data.Linq.DeferredSourceFactory
1.Execute(Object instance) +928
System.Data.Linq.DeferredSource.GetEnumerator() +53
System.Data.Linq.EntitySet1.Load() +112
System.Data.Linq.EntitySet
1.get_Count() +9
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterUser.Write14_User(String
n, String ns, User o, Boolean isNullable, Boolean needType) +5060
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterUser.Write15_User(Object
o) +144

[InvalidOperationException: There was an error generating the XML
document.]
System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle,
String id) +646
System.Xml.Serialization.XmlSerializer.Serialize(TextWriter
textWriter, Object o, XmlSerializerNamespaces namespaces) +72
System.Xml.Serialization.XmlSerializer.Serialize(TextWriter
textWriter, Object o) +10 …

I’m assuming this is because the referenced object no longer exists but what can I do? Somehow copy the object that is returned from the DataContext?

Either way it should be returning XML and not that error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:42:13+00:00Added an answer on May 23, 2026 at 9:42 pm

    You should use view models. Basically you should construct a view model inside the lifetime of the DataContext and pass this view model to the view result (in your case the XmlResult). This view model should be constructed by mapping properties of the actual domain model returned by your datacontext and all this should happen inside this context lifetime. Ayende Rahien has a great series of blog posts about view models (It’s for NHibernate but the problem of disposed context is exactly the same as with EF data datacontexts).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an action that relies on User.Identity.Name to get the username of the
class UserDatastore : IUserDatastore { ... public IUser this[Guid userId] { get { User
MVC Newbie here. I want to get User confirmation before controller action (update a
In ASP MVC3 RedirectToAction kills data in Session...WHY? Consider this code. [HttpGet] public ActionResult
I want to get user input in one page, store that in a php
I'm trying to get user GUID from Active Directory. My code: DirectoryEntry entry =
How to get user roles from Membership provider? Suppose I have a username and
What's the best way to get user input in a C program where the
is there a way to get user input like an inputbox in c#? i
I have an application that needs to hit the ActiveDirectory to get user permission/roles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.