How to use react with asp.net MVC to create a modular site/portal

时间:2018-07-31 09:09:42

标签: javascript asp.net-mvc reactjs webpack reactjs.net

I read all about react, webpack, react router and very other things in all of their documentation and found all menus, links and routes are defined in src and compiled (bundled and minified) by webpack BEFORE publishing.

But I need a mechanism so:

  1. Links and routes generated based on available modules and user requested (for example, a normal user MUST NOT see links for the manager. a solution in react router samples (hiding manager links) is so basic and not for production.

  2. Do this dynamically when user requests for resource

Is there any way to do bundle or add dynamic parts (such as routing configs) to precompiled JavaScripts in runtime?

I'm using asp.net framework MVC

2 个答案:

答案 0 :(得分:0)

如果您想控制什么级别的用户可以看到什么,这是您想要做的:

if(Request.IsAuthenticated){
    if (isAdmin){
        @Html.ActionLink("Link for Managers", "Manager", "SomeFolder")
        }
        @Html.ActionLink("Home", "Index", "Somefolder2")
        @Html.ActionLink("About", "Index", "Somefolder3")
        @Html.ActionLink("Reports", "Index", "Somefolder4")

   }

这将允许所有用户看到“主页”,“关于”和“报告”链接,并且仅允许管理员看到“经理链接”。这将在您的布局中使用razor语法,并且非常容易为用户视图实现其他逻辑。我希望这有助于您走上正确的道路!

答案 1 :(得分:0)

实际上,react是为不同的体系结构制作的,Backend使REST API和react可以是不同的项目,甚至可以分别拥有Server Side Renderingreact调用用于处理应用程序的API,因此,后端可以用任何语言编写。

此外,我对您的情况有所了解,但有一点不同,不幸的是,现在还没有结束,我无法展示自己的情况。我们使用react制作页面,并使用webpack对其进行捆绑,最后将JavaScriptCSS捆绑文件放入页面中。

Razor cshtml文件有点全局,因此react router可以加载用户想要的页面,但是实现不是很好。

前段时间,我看到一个ReactJS.NET网站,该网站声称reactasp.net之间的整合非常可爱和干净。对我来说太奇怪了。至于,它可以为您提供帮助。

希望我的回答对您有所帮助。