Nasir Saeed

Nasir Saeed

  • NA
  • 23
  • 657

View Without Controller Action in MVC

Apr 17 2016 5:23 AM
I read the article http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/view-without-controller-action-in-mvc/
I also want to implement same thing but not able to achieve it. I followed the same steps as in the link. My controller is
public class PublicController : Controller     {         // GET: Public         protected override void HandleUnknownAction(string actionName)         {             try             {                 View(actionName).ExecuteResult(this.ControllerContext);             }              catch (Exception e)             {                 Response.Write("PageNotFound");             }         }     }
My RouteConfig file is
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "NoAction",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Public", action = "HandleUnknownAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
 My master file from where i am calling my view without controller is as
ul>                                 <li><a href="@Url.RouteUrl("Public", new { name = "History.cshtml" })">History</a></li>
 Whenever i run this code i always shows page not found.cshtml file. My views (both history.cshtml and pagenotfound.cshtml are inside Public directory. I am sure i am missing something but i am new to MVC i dont know what is that?

Answers (8)