Q:

Razor code in cshtml (error) page produce error

I throw 404 error from application.
Page for 404 error is in the /ErrorPages/Error_404.cshtml
In this file I have only HTML code and it works fine.
But if I add some razor code it throws configuration error in browser.
I add for example layout or some @Html.ActionLink(...
This is error:

Runtime Error
Description: An application error occurred on the server. The current
  custom error settings for this application prevent the details of the
  application error from being viewed. 
Details: To enable the details of this specific error message to be
  viewable on the local server machine, please create a 
  tag within a "web.config" configuration file located in the root
  directory of the current web application. This  tag
  should then have its "mode" attribute set to "RemoteOnly". To enable
  the details to be viewable on remote machines, please set "mode" to
  "Off".

This is how I produce 404:
public ActionResult Search(string searchTerm)
{
if (city == null)
            {
                throw new HttpException(404, "Some description");               
            }
            else
            {
                return RedirectToAction("Index", "Home", new
                {...
            }
}

And when there is no razor code in error page it works and if not I get message from above.
When I set in web config 'mode=Off' then I get error message:

Description: HTTP 404. The resource you are looking for (or one of its
  dependencies) could have been removed, had its name changed, or is
  temporarily unavailable.  Please review the following URL and make
  sure that it is spelled correctly. 
Requested URL: /CityPage/Navigation/Search

This is value from web config
<customErrors mode="Off">
      <error statusCode="404" redirect="\ErrorPages\Error_cp404.cshtml" />
    </customErrors>

A:

You shouldn't be attempting to render a .cshtml page directly. Those are Razor views. In ASP.NET MVC views are served through controller actions. 
So:
<error statusCode="404" redirect="Errors/Error404" />

where you could have an ErrorsController:
public class ErrorsController: Controller
{
    public ActionResult Error404()
    {
        return View();
    }
}

You may also checkout the following answer for an alternative way to handle errors.
You might also need to add the following if you are running in IIS7+:
<system.webServer>
    ...
    <httpErrors errorMode="Detailed" />
</system.webServer>

