自定义 Aurelia 以使用 .cshtml [英] Customizing Aurelia to use .cshtml

查看:35
本文介绍了自定义 Aurelia 以使用 .cshtml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一篇非常有用的文章,展示了如何将 Razor partials (cshtml) 与 aurelia 结合使用.但是,我无法运行代码并从 RobEisenberg 评论中了解到

I found a very helpful article showing how to use Razor partials (cshtml) with aurelia. However, I could not get the code to run and learned from RobEisenberg comment that

ConventionalViewStrategy.convertModuleIdToViewUrl 

已被弃用.他评论说您想使用 ViewLocator 服务".我关注了 gitHUb 项目,但看不出它与我使用 MVC5 和 Razor Partials 直接相关.所以我很困惑.

had been deprecated. He commented "You want to use the ViewLocator service." I followed the gitHUb project and could not see that it was directly relevant to my using with MVC5 and Razor Partials. So I am confused.

这是我希望我可以调整的示例 main.js 文件,以便将 aurelia 路由到 Home/Index/Index.cshtml 而不是 index.html

This is the example main.js file that I was hoping I could tweak in order to route aurelia to the Home/Index/Index.cshtml instead of index.html

import {LogManager} from "aurelia-framework";
import {ConsoleAppender} from "aurelia-logging-console";
import {ConventionalViewStrategy} from "aurelia-framework";

LogManager.addAppender(new ConsoleAppender());
LogManager.setLevel(LogManager.logLevel.debug);

export function configure(aurelia) {
aurelia.use
    .standardConfiguration()
    .developmentLogging();

ConventionalViewStrategy.convertModuleIdToViewUrl = function(moduleId){
    var moduleName = moduleId.replace("Scripts/", "");
    return `./Templates/${moduleName}`;
}


aurelia.start().then(a => a.setRoot("./Scripts/index", document.body));
}

谁能告诉我如何在 MVC5 项目中设置 aurelia 以使用 .cshtml 而不是 .html 模板?我正在使用 Typescript 和 VS2015

Can anyone tell me how to set up aurelia in an MVC5 project to use .cshtml instead of .html templates? I am using Typescript and VS2015

推荐答案

我刚刚成功地遵循了 http://ilikekillnerds.com/2016/02/using-views-different-locations-aurelia/:

I just successfully followed the approach mentioned at http://ilikekillnerds.com/2016/02/using-views-different-locations-aurelia/:

import {Aurelia, ViewLocator, Origin, Container} from 'aurelia-framework';

export function configure(aurelia: Aurelia, container: Container) {

  aurelia.use
    .standardConfiguration()
    .developmentLogging();

  ViewLocator.prototype.convertOriginToViewUrl = (origin: Origin) => {
    var moduleId: string = origin.moduleId;
    var moduleName = moduleId.split('/')[moduleId.split('/').length - 1].replace('ViewModel', 'View').replace('.js', '').replace('.ts', '');;

    let newViewUrl = `./Templates/${moduleName}`;
    console.log(newViewUrl); // e.g. ./Templates/app

    return newViewUrl;
  }

  aurelia.start().then(() => aurelia.setRoot());
}

这篇关于自定义 Aurelia 以使用 .cshtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆