diff --git a/sample/Util.Samples.Webs/Controllers/HomeController.cs b/sample/Util.Samples.Webs/Controllers/HomeController.cs index 438cf15b..aa12f8b5 100644 --- a/sample/Util.Samples.Webs/Controllers/HomeController.cs +++ b/sample/Util.Samples.Webs/Controllers/HomeController.cs @@ -16,11 +16,12 @@ namespace Util.Samples.Webs.Controllers { } public IActionResult B() { + var a = Request; return Content( "success" ); } public IActionResult Error() { - return View(); + return Content( "" ); } } } diff --git a/sample/Util.Samples.Webs/Models/ErrorViewModel.cs b/sample/Util.Samples.Webs/Models/ErrorViewModel.cs deleted file mode 100644 index 534e0c68..00000000 --- a/sample/Util.Samples.Webs/Models/ErrorViewModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace Util.Samples.Webs.Models -{ - public class ErrorViewModel - { - public string RequestId { get; set; } - public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); - } -} \ No newline at end of file diff --git a/sample/Util.Samples.Webs/Startup.cs b/sample/Util.Samples.Webs/Startup.cs index 8d7a6c00..edc1ad21 100644 --- a/sample/Util.Samples.Webs/Startup.cs +++ b/sample/Util.Samples.Webs/Startup.cs @@ -79,7 +79,7 @@ namespace Util.Samples.Webs { app.UseMvc( routes => { routes.MapRoute( "areaRoute", "{area:exists}/{controller}/{action=Index}/{id?}" ); routes.MapRoute( "default", "{controller=Home}/{action=Index}/{id?}" ); - routes.MapSpaFallbackRoute( "spa-fallback", "{controller=Home}/{action=Index}" ); + routes.MapSpaFallbackRoute( "spa-fallback", new { controller = "Home", action = "Index" } ); } ); } diff --git a/sample/Util.Samples.Webs/Typings/app/a/a.ts b/sample/Util.Samples.Webs/Typings/app/a/a.ts index 5f36e0c4..816b8c26 100644 --- a/sample/Util.Samples.Webs/Typings/app/a/a.ts +++ b/sample/Util.Samples.Webs/Typings/app/a/a.ts @@ -8,8 +8,6 @@ import { util } from "../../util"; export class AComponent { target; constructor() { - util.http.get("/home/b").handle(response => { - this.target = response.text(); - }); + this.target = util.helper.uuid(); } } \ No newline at end of file diff --git a/sample/Util.Samples.Webs/Typings/app/app.module.ts b/sample/Util.Samples.Webs/Typings/app/app.module.ts index def41c60..8ddd0605 100644 --- a/sample/Util.Samples.Webs/Typings/app/app.module.ts +++ b/sample/Util.Samples.Webs/Typings/app/app.module.ts @@ -1,10 +1,10 @@ import { NgModule, Injector } from '@angular/core' -import { FrameworkModule } from '../framework.module' +import { FrameworkModule } from './framework.module' +import { routes as Routes } from './app.routes' import { util } from '../util' import { AppComponent } from './app.component' import { AComponent } from "./a/a"; import { BComponent } from "./b/b"; -import { Router } from '../router' /** * 应用根模块 @@ -14,7 +14,7 @@ import { Router } from '../router' AppComponent, AComponent, BComponent ], imports: [ - FrameworkModule, Router + FrameworkModule, Routes ], providers: [], bootstrap: [AppComponent] diff --git a/sample/Util.Samples.Webs/Typings/app/app.routes.ts b/sample/Util.Samples.Webs/Typings/app/app.routes.ts new file mode 100644 index 00000000..87cd164f --- /dev/null +++ b/sample/Util.Samples.Webs/Typings/app/app.routes.ts @@ -0,0 +1,14 @@ +import { ModuleWithProviders } from '@angular/core' +import { RouterModule, Routes } from '@angular/router' +import { AComponent } from "./a/a"; +import { BComponent } from "./b/b"; + +//路由配置 +let config: Routes = [ + { path: 'a', component: AComponent }, + { path: 'b', component: BComponent }, + { path: '**', redirectTo: '/' } +]; + +//导出路由 +export let routes: ModuleWithProviders = RouterModule.forRoot(config); \ No newline at end of file diff --git a/sample/Util.Samples.Webs/Typings/framework.module.ts b/sample/Util.Samples.Webs/Typings/app/framework.module.ts similarity index 100% rename from sample/Util.Samples.Webs/Typings/framework.module.ts rename to sample/Util.Samples.Webs/Typings/app/framework.module.ts diff --git a/sample/Util.Samples.Webs/Typings/router.ts b/sample/Util.Samples.Webs/Typings/router.ts deleted file mode 100644 index e936b699..00000000 --- a/sample/Util.Samples.Webs/Typings/router.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { RouterModule } from '@angular/router' -import { AComponent } from "./app/a/a"; -import { BComponent } from "./app/b/b"; - -//路由定义 -let router = RouterModule.forRoot([ - { path: 'a', component: AComponent }, - { path: 'b', component: BComponent }, - { path: '**', redirectTo: '/' } -]); - -//导出路由 -export { router as Router } \ No newline at end of file diff --git a/sample/Util.Samples.Webs/Typings/util/angular/http.helper.ts b/sample/Util.Samples.Webs/Typings/util/angular/http.helper.ts index 282b9b5b..62a6a2ef 100644 --- a/sample/Util.Samples.Webs/Typings/util/angular/http.helper.ts +++ b/sample/Util.Samples.Webs/Typings/util/angular/http.helper.ts @@ -8,23 +8,33 @@ import { Observable } from "rxjs"; export class HttpHelper { /** * get请求 - * @param url + * @param url 请求地址 */ - public static get(url: string): HttpHandler { - let response = ioc.get(Http).get(url); - return new HttpHandler(response); + public static get(url: string): HttpRequest { + return new HttpRequest(url); } } /** - * Http响应处理器 + * Http请求操作 */ -export class HttpHandler { +class HttpRequest { + private _headers; + + private _response: Observable; + /** - * Http响应处理器 - * @param response 响应观察者 + * 初始化Http请求操作 + * @param url 请求地址 */ - constructor(private response: Observable) { + constructor(private url: string) { + this._headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded' }); + } + + private request() { + if (this._response) + return; + this._response = ioc.get(Http).get(this.url, { headers: this._headers}); } /** @@ -32,6 +42,17 @@ export class HttpHandler { * @param handler 响应处理函数 */ public handle(handler: (value: Response) => void) { - this.response.subscribe(handler); + this.request(); + this._response.subscribe(handler); } +} + +/** + * Http内容类型 + */ +export enum HttpContentType { + /** + * application/json + */ + Json } \ No newline at end of file diff --git a/sample/Util.Samples.Webs/Typings/util/common/helper.ts b/sample/Util.Samples.Webs/Typings/util/common/helper.ts index e175524f..9ebb79ee 100644 --- a/sample/Util.Samples.Webs/Typings/util/common/helper.ts +++ b/sample/Util.Samples.Webs/Typings/util/common/helper.ts @@ -1,7 +1,4 @@ -//============== 公共操作库 ================= -//Copyright 2017 何镇汐 -//Licensed under the MIT license -//============================================ +import { UUID } from './uuid' /** * 是否未定义 @@ -78,4 +75,11 @@ export let isArray = (value): boolean => { */ export let isEmptyArray = (value): boolean => { return isArray(value) && value.length === 0; +} + +/** + * 创建唯一标识 + */ +export let uuid = (): string => { + return UUID.UUID(); } \ No newline at end of file diff --git a/sample/Util.Samples.Webs/Typings/util/common/uuid.ts b/sample/Util.Samples.Webs/Typings/util/common/uuid.ts new file mode 100644 index 00000000..3b8fa218 --- /dev/null +++ b/sample/Util.Samples.Webs/Typings/util/common/uuid.ts @@ -0,0 +1,36 @@ +//À´Դ£ºhttps://github.com/wulfsolter/angular2-uuid/blob/master/src/uuid.ts +"use strict"; + +export class UUID { + + public static UUID(): string { + if (typeof (window) !== "undefined" && typeof (window.crypto) !== "undefined" && typeof (window.crypto.getRandomValues) !== "undefined") { + // If we have a cryptographically secure PRNG, use that + // http://stackoverflow.com/questions/6906916/collisions-when-generating-uuids-in-javascript + let buf: Uint16Array = new Uint16Array(8); + window.crypto.getRandomValues(buf); + return (this.pad4(buf[0]) + this.pad4(buf[1]) + "-" + this.pad4(buf[2]) + "-" + this.pad4(buf[3]) + "-" + this.pad4(buf[4]) + "-" + this.pad4(buf[5]) + this.pad4(buf[6]) + this.pad4(buf[7])); + } else { + // Otherwise, just use Math.random + // https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript + // https://stackoverflow.com/questions/11605068/why-does-jshint-argue-against-bitwise-operators-how-should-i-express-this-code + return this.random4() + this.random4() + "-" + this.random4() + "-" + this.random4() + "-" + + this.random4() + "-" + this.random4() + this.random4() + this.random4(); + } + } + + private static pad4(num: number): string { + let ret: string = num.toString(16); + while (ret.length < 4) { + ret = "0" + ret; + } + return ret; + } + + private static random4(): string { + return Math.floor((1 + Math.random()) * 0x10000) + .toString(16) + .substring(1); + } + +} diff --git a/sample/Util.Samples.Webs/Util.Samples.Webs.csproj b/sample/Util.Samples.Webs/Util.Samples.Webs.csproj index 2e615d50..ced40ce6 100644 --- a/sample/Util.Samples.Webs/Util.Samples.Webs.csproj +++ b/sample/Util.Samples.Webs/Util.Samples.Webs.csproj @@ -48,6 +48,10 @@ + + + + diff --git a/sample/Util.Samples.Webs/Views/Shared/Error.cshtml b/sample/Util.Samples.Webs/Views/Shared/Error.cshtml deleted file mode 100644 index a77b1514..00000000 --- a/sample/Util.Samples.Webs/Views/Shared/Error.cshtml +++ /dev/null @@ -1,17 +0,0 @@ - -@{ - ViewData["Title"] = "Error"; -} - -

Error.

-

An error occurred while processing your request.

- - - -

Development Mode

-

- Swapping to Development environment will display more detailed information about the error that occurred. -

-

- Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application. -