Sekory
Sekory

Reputation: 143

How to make form page invalid?

I am creating pages in asp.net. I created WebForm called Form.aspx. Now I want all request *.htm request to load this pages where I will do what I need (maybe not the best approach but it works as needed for me). So I created something like this:

routes.MapPageRoute(null, "{file}.htm", "~/Pages/Form.aspx");
routes.MapPageRoute(null, "{folder}/{file}.htm", "~/Pages/Form.aspx");

Now everything like http://example.com/whatever.htm or http://example.com/whatever/whatever.htm is redirected to my Form.aspx. But this Form.aspx doesn't have any meaning on it’s own. So following page is nonsense http://example.com/Pages/Form.aspx. How can I make it invalid? So it would show to me something like "Error HTTP 404.0 – Not Found". I want the same behaviour as if I would wrote "http://example.com/doesntexist.aspx". I don't want to do any redirection (only if no other option exists). I so far tried only something like this (which doesn't work):

routes.MapPageRoute(null, "Pages/Form.aspx", "~/doesntexist.aspx");

It doesn't do anything... Any help appreciated.

Upvotes: 0

Views: 158

Answers (1)

user3806621
user3806621

Reputation: 288

In Global.asax add this code:

...

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        string requestPath = Request.RawUrl.Trim().ToLower();
        HttpApplication app = sender as HttpApplication;

        if (!IsLocalRequest(app.Context.Request))
        {
            if (requestPath.IndexOf("form.aspx") > 0)
            {
                throw new HttpException(404, "Error HTTP 404.0 – Not Found.");
            }
        }
    }

// This method determines whether request came from the same IP address as the server.
public static bool IsLocalRequest(HttpRequest request)
{
    return request.ServerVariables["LOCAL_ADDR"] == request.ServerVariables["REMOTE_ADDR"];
}

...

Upvotes: 1

Related Questions