2009-01-14 65 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")