首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过编程在MVC Controller文件夹中添加Controller类

如何通过编程在MVC Controller文件夹中添加Controller类
EN

Stack Overflow用户
提问于 2019-05-17 15:02:05
回答 1查看 27关注 0票数 0

我在MVC中创建了动态控制器,并在Controller文件夹中创建了控制器,但通过手动添加动态控制器可以很好地反映在Solution-Explorer Controller文件夹中,所以这里我的问题是如何在controller文件夹中反映Controller类

代码语言:javascript
复制
        StringBuilder sb = new StringBuilder();            
        sb.Append("using System;" + Environment.NewLine);
        sb.Append("using System.Collections.Generic;" + Environment.NewLine);
        sb.Append("using System.Data;" + Environment.NewLine);
        sb.Append("using System.Data.SqlClient;" + Environment.NewLine);
        sb.Append("using System.Dynamic;" + Environment.NewLine);
        sb.Append("using System.Linq;" + Environment.NewLine);
        sb.Append("using System.Text;" + Environment.NewLine);
        sb.Append("using System.Web.Mvc;" + Environment.NewLine);

        sb.Append("namespace Testing_MVC.Controllers" + Environment.NewLine);
        sb.Append("{" + Environment.NewLine);

        sb.Append("public class " + ctrl + "Controller" + " : Controller" + Environment.NewLine);
        sb.Append("{" + Environment.NewLine);

        sb.Append("public ActionResult Index()" + Environment.NewLine);
        sb.Append("{" + Environment.NewLine);
        sb.Append("return View();" + Environment.NewLine);
        sb.Append("}" + Environment.NewLine);

        sb.Append("}" + Environment.NewLine);

        sb.Append("}" + Environment.NewLine);

        var dir = Server.MapPath("~\\Controllers");
        var file = System.IO.Path.Combine(dir, ctrl + "Controller" + ".cs");
        System.IO.Directory.CreateDirectory(dir);
        System.IO.File.WriteAllText(file, sb.ToString());

        return this.RedirectToAction("Index", ctrl, new { id = 1 });

通过将html调用为MVC路由:

代码语言:javascript
复制
window.location.href = '@Url.Action("common_dll", "Home")?ctrl=Test';

需要通过C#程序自动在控制器文件夹中反映该类,而无需手动。

EN

回答 1

Stack Overflow用户

发布于 2019-05-17 15:07:20

代码语言:javascript
复制
I mention that the anwser does not belong to me. In the past I had issues with the MVC. 
Here is the complete question history.
https://stackoverflow.com/questions/9988634/ajax-call-into-mvc-controller-url-issue

 In order for this to work that Javascript must be placed within a Razor view so that the line

        @Url.Action("Action","Controller")

        is parsed by Razor and the real value replaced.

        If you don't want to move your Javascript into your View you could look at creating a settings object in the view and then referencing that from your Javascript file.

        e.g.

        var MyAppUrlSettings = {
            MyUsefulUrl : '@Url.Action("Action","Controller")'
        }

        and in your .js file

        $.ajax({
         type: "POST",
         url: MyAppUrlSettings.MyUsefulUrl,
         data: "{queryString:'" + searchVal + "'}",
         contentType: "application/json; charset=utf-8",
         dataType: "html",
         success: function (data) {
         alert("here" + data.d.toString());
        });

        or alternatively look at levering the framework's built in Ajax methods within the HtmlHelpers which allow you to achieve the same without "polluting" your Views with JS code.

        Dont forget the slash before 'Controller'. It will create an incorrect URL. byut yes, this is what I use now. 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56181208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档