Link to home
Start Free TrialLog in
Avatar of Allan
AllanFlag for United States of America

asked on

can mvc5 coexists with razor

Hi Experts.

Have tried googling "can mvc5 coexists with razor"; but didn't find anything helpful.

So, in our MVCr project we created a folder called "Pages" and added a razor3 file called "Test.cshtml".
User generated image The project builds and when trying to navigate to the razor page:

User generated image
any ideas?

TIA!
Avatar of Misha
Misha
Flag of Russian Federation image

mvc - is template Mode-View-Controller.
Your path, which you posted here means, that you call controller with name "Page" and method of this controller with name "test".
It doesn't exist.
If you want to work with this template, you should create controller class in the folder "Controllers", add method which return View() (type "ActionResult"),
and then right click  on this method in MS Visual studion and select "create view".
In this case this view will be open, when you call this controller method,
Start to learn asp net mvc with this links:
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/adding-a-controller
https://www.asp.net/mvc/overview
Avatar of Allan

ASKER

Thanks Misha.

In asp.net core 2.0 if you pass the name of the razor page to the HTTP request the MVC Framework will look at the ‘Pages’ folder and find the matching ‘.cshtml’ file called “Greeting”. So, the MVC pattern can co-exists with a razor page.

User generated image
 However, we’re not sure, if it’s even possible, to wire, it so that the routing engine will see that it’s not a Controller, but a razor page. In MVC5 the method “Application_Start” of the file “Global.asax.cs” contains a call the routing definition: “RouteConfig.RegisterRoutes(RouteTable.Routes);”.

The question is if you know and how if we can get the MVC pattern to co-exists with razor pages.
Does this make sense? Thanks!
If I correct understand you, you asked about ASP NET MVC 5, not about ASP NET CORE 2.0.
In ASP NET CORE you can route to page folder directly. Like in this small artcile:
https://exceptionnotfound.net/setting-a-custom-default-page-in-asp-net-core-razor-pages/
In ASP NET MVC you can  route only to controller and method of controller, which return view.
You can create custom route like in this article:
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/creating-custom-routes-cs
But in this case you also need to specify controller and its method.

In ASP NET MVC you can use Razor syntax to get directly data from your model, create variables and use C# code in html (cshtml files)
You can read about using razor in MVC in this article:
https://docs.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-razor-syntax-c
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Allan

ASKER

Thanks Miguel. Wanted someone to confirm it can be done my creating a custom route. What we wanted to do for html stuffs is start going in the direction of using Razor and for use mvc for web api.
Avatar of Allan

ASKER

Thank you for your time!