How to use ASP.Net MVC View.csthml as Angular View instead of .html

I am trying to use Angulars $ routeProvider to load templates into a container in a .cshtml file which is in the view folder.

Im getting 404 with this code I am Angular:

.when('#/second',{ urlTemplate:"Views/second.cshtml",controller:"MainController"})

Agular cannot find second.cshtml file. What for? Can't I use .cshtml files?

+4
source share
2 answers

To configure .cshtml for the client, several steps are required.

I created a sample project called AngularAspNet on GitHub .

1) App Angular. , .

2) .cshtml App/xxx/Components/. , .

enter image description here

Angular View Component ( JavaScript). , View , . .js Views, .chtml App (Angular Script).

1) web.config BlockViewHandler .

<?xml version="1.0"?>

<configuration>
  <configSections>
  ...        
  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*.cshtml" verb="*" 
          preCondition="integratedMode" 
          type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
   ...
</configuration>

4) .

public class NgController : Controller
{
    public PartialViewResult RenderComponents(string feature, string name)
    {
        return PartialView($"~/App/{feature}/Components/{name}");
    }
}

5) .

routes.MapRoute(
    name: "Components",
    url: "{feature}/components/{name}",
    defaults: new { controller = "Ng", action = "RenderComponents" });

AngularJS ASP.NET MVC 5 Matt Honeycutt

AngularJS ASP.NET MVC

+3

.cshtml.

, .html , MVC: /MyController/MyAction.

0

Source: https://habr.com/ru/post/1657075/


All Articles