5

Ich habe asp. net mvc4 Projekt. Angularjs ist integriert. Ich habe bereits HTML-Seiten und WEB-APIs 2 gemäß den vorherigen Anforderungen erstellt.angularjs mit cshtml Seite nur nicht html Seiten mit web api 2

Jetzt aus irgendeinem Grund muss ich mit CSHTML-Seiten gehen. zuvor hatte ich nur web api projektvorlage, so konnte ich nicht mit cshtml seiten als nur MVC controller kann teilseite (cshtml) zurückgeben aber ich war nur web apis ...

So geändert ganze projektvorlage und so jetzt kann ich Mvc-Controller haben ... ...

kurz gesagt, zur Zeit habe ich mvc4 Projekt mit bereits eingebauten Web-APIs und HTML-Seiten .....

ich möchte nur CSHTML Seiten anstelle von HTML-Seiten verwenden Rest Dinge sollten so sein, wie sie sind ...

Ist es möglich?

Ich mag albern klingen, aber es ist gerade jetzt notwendig. Hilfe würde sehr geschätzt werden ...

Web API, nur cshtml Seiten und angularjs. nicht web apis, html seiten und angularjs.

Wenn ich html Seiten durch cshtml, ersetze, wie würde die Winkelroute betroffen sein?

Antwort

3

Nun, das ist, wie ich es benutze.

Es gibt index.cshtml wie diese

@using System.Web.Optimization 
@inherits System.Web.Mvc.WebViewPage 
@{ Layout = null; }<!DOCTYPE html> 
<html data-ng-app="MyProject"> 
<head> 
    <title data-ng-controller="TitleCtrl" data-ng-bind="Title">My Project</title> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <meta http-equiv="cache-control" content="max-age=0" /> 
    <meta http-equiv="cache-control" content="no-cache" /> 
    <meta http-equiv="expires" content="0" /> 
    <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> 
    <meta http-equiv="pragma" content="no-cache" /> 

    @Styles.Render("~/Content/min/css") 
</head> 
<body class="body" id="body"> 

    <div class="body" data-ui-view="body"></div> 

    @Scripts.Render("~/js/angular") 
    @Scripts.Render("~/js/MyProject") 
</body> 
</html> 

HINWEIS: Winkel UI-Router verwenden, das ist, warum ui-view in palce ist

Aber noch muss es sein HomeController:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     // below I do some tricks to make app running on 
     // http://mydomain/app as well as on 
     // http://mydomain/app/ (see the/at the end) 

     var root = VirtualPathUtility.ToAbsolute("~/"); 
     var applicationPath = Request.ApplicationPath; 
     var path = Request.Path; 
     var hasTraillingSlash = root.Equals(applicationPath 
             , StringComparison.InvariantCultureIgnoreCase) 
       || !applicationPath.Equals(path 
             , StringComparison.InvariantCultureIgnoreCase); 
     if (!hasTraillingSlash) 
     { 
      return Redirect(root + "#"); 
     } 

     // my view is not in Views, but in the root of a web project 
     return View("~/Index.cshtml"); 
    } 
} 

So kann ich die Macht der Bundle-Konfiguration (javascript, css) ... beim Starten Winkel bei http://mydomain/app oder http://mydomain/app/ verwenden. prüfen auch ähnlich here

Auch in global.asax, sollten wir ASP.NET MVC Configura:

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.IgnoreRoute("fonts*.woff"); 

    routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new {controller = "Home", action = "Index", id =UrlParameter.Optional} 
    ); 
} 

während Web-API sein sollte:

const string IdPattern = @"\d+|[A-Za-z]{1,2}"; 
public static void SetRouting(HttpConfiguration config) 
{ 
    // some custom routes 
    ...    

    // and a default one 
    config.Routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}/{id}" 
     , constraints: new { id = IdPattern } 
     , defaults: new { id = RouteParameter.Optional } 
    ); 
+0

können sagen, dass ich Menü, das hat Produktseite. nach dem Erreichen des Dashboards, wenn ich auf die Produktmenüliste klicke, wie würde es mich auf die Seite products.cshtml umleiten? – micronyks

+0

jetzt habe ich ui router wie folgt eingestellt ... url: '/ products', template: '/ products. html ' – micronyks

+0

Ich erweiterte meine Antwort mit Routing für MVC (Home/Index) und Web API. Der Trick ist, dass, während der Home/Index geladen wird und die index.csthml gerendert wird ... wir keine Umleitungen benötigen. Wir werden SPA haben ... jetzt ist es Angular Job, alle Ansichten zu laden ... –