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

  • Home
  • SEARCH
  • 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 7084565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:19:02+00:00 2026-05-28T07:19:02+00:00

I am working with the sample ASP.NET MVC 3 application. I am working on

  • 0

I am working with the sample ASP.NET MVC 3 application. I am working on a proof of concept for an API for a project I’m working on, by sending log in requests using HttpWebRequests. I am submitting data as JSON and am using the Content Type “application/json”.

Here is the method I have:

[HttpPost]
[Api]
public bool ApiLogOn(LogOnModel model)
{
    if (ModelState.IsValid && model != null)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            return true;
        }
    }

    return false;
}

The problem is that even though ModelState contains all the keys and values that I expect to see (e.g. Dictionary contains { “UserName”=”username”, “Password”=”password”, “RememberMe”=true } which are the three properties of the LogOnModel class), the object model is null every time I call the method.

I do not have this problem when using “application/x-www-form-urlencoded”. I have also tried with and without adding

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

to my Application_Start method.

Here is the code I am using to send requests to the method above:

var credentials = new LogOnModel() { UserName = "username", Password = "password", RememberMe = true };
var result = PostData<LogOnModel, bool>(@"url goes here", credentials);

protected R PostData<T, R>(string uri, T postData)
{
    var request = WebRequest.Create(uri) as HttpWebRequest;
    request.Headers.Add("X-Requested-With", X_REQUESTED_WITH_HEADER);
    request.Accept = "application/json";
    request.Method = "POST";
    request.CookieContainer = cookieJar;
    request.ContentType = "application/json";

    var serializerT = new DataContractJsonSerializer(typeof(T));
    var serializerR = new DataContractJsonSerializer(typeof(R));

    if (postData != null)
    {
        using (var stream = request.GetRequestStream())
        {
            serializerT.WriteObject(stream, postData);
        }
    }

    try
    {
        var response = request.GetResponse() as HttpWebResponse;

        if (response.StatusCode == HttpStatusCode.OK)
        {
            using (var stream = response.GetResponseStream())
            {
                var result = (R)serializerR.ReadObject(stream);
                return result;
            }
        }
        else
        {
            return default(R);
        }
    } catch { return default(R); }
 }
  • 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-28T07:19:03+00:00Added an answer on May 28, 2026 at 7:19 am

    It turns out that removing the [Api] Attribute solved my problem. If anyone is interested, it came from this url: http://mvcapi.codeplex.com/

    The point was such that we could use methods that had both Views (for our web page) and JSON results (for our API calls). If anyone has any other ideas for this (aside from the obvious if-this-return-view-else-return-json) please feel free to comment below, I’d like to hear them.

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

Sidebar

Related Questions

using VS 2008 i created a clean asp.net MVC application project and changed nothing
I'm working on a sample project built from the ASP.NET Web Application template (web
I've been working on a ASP.NET MVC project with LinqToSql. The application has 3
I am working on e-commerce application using asp.net MVC with C#. I need to
I'm looking for a working sample of an ASP.NET MVC web application that uses
I'm working through a sample .Net MVC application. I've added the aspnet membership api
I have an existing ASP.NET MVC application with some sample data in the SQL
I was trying to download some ASP.NET MVC Sample application to learn MVC. I
I've been building my sample asp.net application using VWD2008 and the development virtual server
I have written a simple test application using asp.net mvc with C#. The application

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.