IronPython on ASP.NET MVC

前端 未结 3 1354
無奈伤痛
無奈伤痛 2021-01-30 18:03

Has anyone tried ASP.NET MVC using IronPython? Having done a lot of Python development recently, it would be nice to continue with the language as I go into a potential ASP.NET

3条回答
  •  深忆病人
    2021-01-30 18:25

    I'm currently working on this. It already supports a lot of things: https://github.com/simplic-systems/ironpython-aspnet-mvc

    more information on this:

    Import the aspnet module

    import aspnet
    

    You can write your own controller

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

    You can automatically register all controller

    aspnet.Routing.register_all()
    

    You can use different http-methods

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

    And there is much more. Here is a very short example

    # ------------------------------------------------
    # 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")
    

提交回复
热议问题