Customizing Aurelia to use .cshtml

2019-03-02 09:33发布

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条回答
再贱就再见
2楼-- · 2019-03-02 10:32

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());
}
查看更多
登录 后发表回答