Customizing Aurelia to use .cshtml

一个人想着一个人 提交于 2019-12-02 09:39:32

问题


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 

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.

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));
}

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


回答1:


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());
}


来源:https://stackoverflow.com/questions/37870145/customizing-aurelia-to-use-cshtml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!