rumi
rumi

Reputation: 3298

how to add a view without adding an action method in the controller in mvc

We have a reasonably big MVC website and in the middle of a sprint we have to deploy a view containing static text. The current state of the website is not ready for live deployment and we need to do a lot of testing before its ready. Can we possibly deploy this view without adding an action method in the controller directly on the webserver. e.g. the new view name is Policy.cshtml it resides in Views/Home/ folder and we want to add an anchor link to the footer of a the layout view like

<a href="/home/Policy">Policy</a>

Upvotes: 1

Views: 892

Answers (3)

Emmanuel DURIN
Emmanuel DURIN

Reputation: 4913

A solution to bypass controllers is to enable Web Pages in web.config :

<appSettings>
  <add key="webpages:Enabled" value="true" />
</appSettings>

Then you can easily access a page without typing the action name

With Hello.cshtml at root of your site, you can access it with :

 http://localhost:59051/Hello
 or
 http://localhost:59051/Hello.cshtml

But take care, you can't by default access directly to views under Views directory.

Reference:

https://learn.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/aspnet-web-pages-razor-faq

Regards

Upvotes: 2

Sachu
Sachu

Reputation: 7766

In MVC Framework, the controller class includes a method HandleUnknownAction() that executes whenever we attempt to invoke an action (or when we request a view which has no matching action method) on a controller that does not exist.

protected override void HandleUnknownAction(string actionname)
{
  this.View(actionname).ExecuteResult(this.ControllerContext);
}

Upvotes: 1

Ilya Sulimanov
Ilya Sulimanov

Reputation: 7856

All request must be passed in controller. You can add Fake Action or render view on current page. And you can show/hide this div by means of javascript.

<a href="javascript:" id="PolicyLink">Policy</a>
<div id ="Policy" style="display:none;">
    @Html.Partial("Policy")
</div>
<script>
    $("#PolicyLink").on("click",function()
    {
      $("#Policy").show();
    });
</script>

Upvotes: 0

Related Questions