MVC Rendering (RenderPartial, RenderAction) Html from another MVC Application

前端 未结 2 880
慢半拍i
慢半拍i 2021-01-14 06:14

I am working in an environment with many teams who are responsible for specific content on pages. Each team is sharing specific information (common class libraries, and mas

相关标签:
2条回答
  • 2021-01-14 06:44

    In principal yes, though your question is a little vague.

    Have a look at "portable areas" within MvcContrib on codeplex. This technique allows separate teams to develop separate MVC apps that would then be orchestrated by a central application.

    0 讨论(0)
  • 2021-01-14 06:44

    No, RenderPartial/RenerAction can only load views that it can access via reflection, not via HTTP requests to external resources.

    If the MVC app for 'ads.mydomain.com' is available to you at compile them then you can utilise its resources via Areas, however it won't pickup the changes if they release a new version to the 'ads.mydomain.com' website without you getting their latest assembly and re-compiling and deploying your app as well.

    You can do similar stuff with AJAX where you can load a fragment from another site, however it wouldn't be done server side, and would require the client to have javascript enabled. Also the model would need to be converted to JSON and posted to the request, so its a bit of a hacky solution.

    You could write an extension method (lets call it Html.RenderRemote) which does all the work for you of creating an http connection to the target and requests the URL. You'd have to serialize the model and send it as part of the request.

    public static string RenderRemote(this HtmlHelper, string url, object model)
    {
        // send request to 'url' with serialized model as data
        // get response stream and convert to string
        // return it
    }
    

    You could use it as :

    <%= Html.RenderRemote('http://ads.mydomain.com', Model');
    

    You wouldn't be able to take advantage of the routes on the remote domain, so you'd have to construct the literal URL yourself, which means if they change your routing rules your URL won't work anymore.

    0 讨论(0)
提交回复
热议问题