繁体   English   中英

最简单的ASP.NET和AnjularJS主页

[英]Simplest home page with ASP.NET and AnjularJS

我想使用AngularJS和ASP.NET Web API构建SPA。 关于前端网页,我想尽可能地限制asp.net的含义,并将所有逻辑转移到Angular中,Web API唯一提供的就是REST服务。 我创建了一个index.html页面,该页面具有一些角度,可以从服务器加载基本列表。

但是我的index.html是使用ex访问的。 http://localhost:1234/app/index.html ,现在我想从http://localhost:1234/看到我的index.html ,如果我从此访问一些随机链接,还会得到一个自定义错误页面主办。

我需要ASP.NET来执行此操作吗? 我想尽可能地限制使用ASP.NET,仅限制基本必需的东西。 我对此完全陌生。

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <clear />
      <add
          name="StaticFile"
          path="*" verb="*"
          modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule"
          resourceType="Either"
          requireAccess="Read" />
    </handlers>
    <staticContent>
      <mimeMap fileExtension=".*" mimeType="application/octet-stream" />
    </staticContent>
    <defaultDocument enabled="true">
      <files>
        <clear/>
        <add value="index.html"/>
      </files>
    </defaultDocument>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

还添加了路由:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
        }

页面错误:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /

好的角度仅在客户端进行路由,因为您的url需要您仍然需要在服务器端进行一些配置。

这应该可以解决问题:

将此添加到web.config:

<system.webServer>
<defaultDocument>
  <files>
    <clear/>
    <add value="(location of index.html)"/>
  </files>
</defaultDocument>

至于错误(其中有404个错误),可以在此处找到答案。 仅当有人尝试获取不存在的url(在已加载angular之前)时,这才适用。

但是,一旦加载了angular,就可以使用config来完成所有操作,在其中配置$ routeProvider:

$routeProvider.when('/someUrl', {templateUrl: 'url/templatefile.html', controller: 'templateController'})
              .when('/my404route', {templateUrl: 'url/my404file.html', controller: 'my404Controller'})
              //when returns $routeprovider so you can chain "whens"
              //finnaly add "otherwise"
              .otherwise({redirectTo: '/my404route'});
              //or just route to root with '/' if you won't handle unknown routes

默认情况下,Visual Studio将站点部署在子文件夹中。 要更改它,请右键单击您的Web项目文件/属性。 然后在“ Web”选项卡中,将项目URL指定为http://localhost:1234

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM