IronPython在ASP.NET MVC上的应用

24

有人尝试使用IronPython来进行ASP.NET MVC吗?最近我做了很多Python开发,如果能在一个可能的ASP.NET MVC项目中继续使用这种语言会很好。

我特别想利用Python动态特性与.NET功能(如LINQ)相结合,想知道这是否可行。另一种可能适用于某些动态编程的方法是使用带有dynamic关键字的C# 4.0。

有什么想法或经验吗?

3个回答

14

这些链接示例似乎是关于WebForms而不是ASP.Net MVC。 - Abhijit Rao
1
@Abhijit,不幸的是,CodePlex的URL似乎有限的生命周期。自从我发布这个答案以来的两年多时间里,它们已经破坏了链接。 - Craig Stuntz
有人在生产环境中使用这个吗? - Petrus Theron

8

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

该页面包含以下文章:

  • 为ASP.NET MVC创建简单的IronPython ControllerFactory
  • 为ASP.NET MVC创建简单的IronPython ActionFilter
  • 为ASP.NET MVC创建简单的IronPython Route Mapper
  • 为ASP.NET MVC创建不显眼的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")

虽然这理论上回答了问题,但最好在此处包含答案的基本部分,并提供参考链接。 - Bhargav Rao
@BhargavRao的回答已经改进了,现在符合要求了吗? - BendEg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接