0

In Asp.net Mvc 5 How we can redirect another page if controller name is invalid in url like http://localhost:51056/free

1

2 Answers 2

1

Redirecting to a controller name that doesn't exist will always return an HTTP 404 (Not Found) response.

Probably the best way would be to set up a redirection rule on your server that redirects /free to some other path that does exist.

Sign up to request clarification or add additional context in comments.

Comments

1

You can set configuration in web.config like

<customErrors mode="On" >
       <error statusCode="404" redirect="/404.shtml" />
</customErrors>

or You can do this in Global.asax

if (Context.Response.StatusCode == 404) {
  // handle this
}

1 Comment

In theory you could have this redirect to a a different controller than the one requested and that would handle the 404 in a way that would allow the user to continue working on your site. So if /free is a deprecated link that people are still somehow using (from an outdated share somewhere else maybe) then you could do this to get them to the new url. Only drawback would be that it'd work that way for EVERY 404...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.