2009-01-14 63 views
24

有没有人试过使用IronPython的ASP.NET MVC?最近完成了很多Python开发,当我进入潜在的ASP.NET MVC项目时,继续使用该语言会很好。ASP.NET MVC上的IronPython

我特别感兴趣的是利用LINQ等.NET特性来利用Python的动态方面,并且想知道这是否可能。另一条可能适用于某些动态编程的路线是C#4.0及其关键字dynamic

想法,经验?

回答

14

是的,there is an MVC example from the DLR team。您可能也有兴趣Spark

+0

链接的样本似乎是有关的WebForms,而不是ASP.Net MVC – 2011-05-05 07:07:34

+1

@Abhijit,不幸的CodePlex网址似乎有一个有限的寿命。在我发布这个答案后的2年中,他们打破了这个链接。 – 2011-05-05 12:47:30

8

在ASP.NET MVC使用IronPython的:http://www.codevoyeur.com/Articles/Tags/ironpython.aspx

此页面包含以下条款:

  • 一个简单的IronPython的ControllerFactory ASP.NET MVC的
  • 一个简单的IronPython ActionFilter的ASP.NET MVC
  • 简单的IronPython用于ASP.NET MVC的IronPython路由映射器
  • 用于ASP.NET MVC的UnPtrusive IronPython ViewEngine
5

我目前正在研究这个问题。它已经支持了很多东西:本https://github.com/simplic-systems/ironpython-aspnet-mvc

的更多信息:

导入aspnet模块

import aspnet 

你可以写你自己的控制器

class HomeController(aspnet.Controller): 

    def index(self): 
     return self.view("~/Views/Home/Index.cshtml") 

可以自动注册所有控制器

aspnet.Routing.register_all() 

您可以使用不同的HTTP方法

@aspnet.Filter.httpPost 
    def postSample(self): 
     return self.view("~/Views/Home/Index.cshtml") 

而且还有更多。这里是一个非常简单的例子

# ------------------------------------------------ 
# This is the root of any IronPython based 
# AspNet MVC application. 
# ------------------------------------------------ 

import aspnet 

# Define "root" class of the MVC-System 
class App(aspnet.Application): 

    # Start IronPython asp.net mvc application. 
    # Routes and other stuff can be registered here 
    def start(self): 

     # Register all routes 
     aspnet.Routing.register_all() 

     # Set layout 
     aspnet.Views.set_layout('~/Views/Shared/_Layout.cshtml') 

     # Load style bundle 
     bundle = aspnet.StyleBundle('~/Content/css') 
     bundle.include("~/Content/css/all.css") 

     aspnet.Bundles.add(bundle) 

class HomeController(aspnet.Controller): 

    def index(self): 
     return self.view("~/Views/Home/Index.cshtml") 

    def page(self): 
     # Works also with default paths 
     return self.view() 

    def paramSample(self, id, id2 = 'default-value for id2'): 
     # Works also with default paths 
     model = SampleModel() 
     model.id = id 
     model.id2 = id2 
     return self.view("~/Views/Home/ParamSample.cshtml", model) 

    @aspnet.Filter.httpPost 
    def postSample(self): 
     return self.view("~/Views/Home/Index.cshtml") 

class SampleModel: 
    id = 0 
    id2 = '' 

class ProductController(aspnet.Controller): 

    def index(self): 
     return self.view("~/Views/Product/Index.cshtml")