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 4574414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:55:11+00:00 2026-05-21T19:55:11+00:00

I have a asp.net mvc route that is taking a url and doing a

  • 0

I have a asp.net mvc route that is taking a url and doing a simple get and return the status code from the request.

<AcceptVerbs(HttpVerbs.Post)> _
Public Function ValidateUrlStatusCode(ByVal url As String) As ActionResult
   Dim code As Integer = 0

   Try
      Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
      request.Method = "GET"
      request.AllowAutoRedirect = True
      Using response As HttpWebResponse = request.GetResponse
         response.Close()
         code = response.StatusCode
      End Using
   Catch ex As Exception
      code = HttpStatusCode.InternalServerError
   End Try
   Return Content(code, "text/plain")
End Function

Now if I use firefox (using Firebug) and go to the url http://www.facebook.com/blah.html, I get the expected 404 returned. However if I use my application to call the mvc route via an ajax call, I get 200. If I set the request object’s AllowAutoRedirect to false, I get 302. I never get a 404. I am verifying this once again through Firebug. Can anyone point out what I am doing wrong?

Thanks!

  • 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-21T19:55:12+00:00Added an answer on May 21, 2026 at 7:55 pm

    If you use FaceBook make sure you set the user agent or the site will redirect you to a standard HTML page explaining you to do so (thus the 200 status code):

    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
    

    Also when a status code different than 200 is returned from the HttpWebRequest an exception will be thrown, and more specifically a WebException. So you need to trap this WebException and inside the Response property containing the HttpWebResponse you will find the 404 StatusCode.

    Also I would probably use a WebClient to simplify the code:

    Public Function ValidateUrlStatusCode(url As String) As ActionResult
        Dim code = 0
        Try
            Using client = New WebClient()
                client.Headers(HttpRequestHeader.UserAgent) = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0"
                Dim response = client.DownloadString(url)
            End Using
        Catch ex As WebException
            Dim httpResponse = TryCast(ex.Response, HttpWebResponse)
            If httpResponse IsNot Nothing Then
                code = CInt(httpResponse.StatusCode)
            End If
        End Try
    
        Return Content(code.ToString(), "text/plain")
    End Function
    

    And on the client:

    <script type="text/javascript">
        $.ajax({
            url: '@Url.Action("ValidateUrlStatusCode")',
            type: 'POST',
            data: { url: 'http://www.facebook.com/blah.html' },
            success: function (result) {
                alert(result);
            }
        });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a route defined last in my ASP.Net MVC 2 app that will
I have an ASP.NET MVC application with a route that allows searching for stuff
I have an asp.net mvc app with a route that allows users to request
I have ASP.NET 4 project (not MVC). I need to create url route based
Is it possible to have an ASP.NET MVC route that uses subdomain information to
I have a route in my asp.net mvc 2 site that looks like this
I have an Asp.Net MVC application that works in the vs.net development web server.
I have an ASP.NET MVC server app and client code which uses jquery. I'd
I have a ASP.NET MVC application that runs in both IIS 6 and 7.
I currently have an ASP.NET MVC application that exists. I want to add a

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.