我在MVC中创建了动态控制器,并且该文件位于“控制器”文件夹中,但是通过手动添加动态控制器工作正常,创建了未反映在Solution-Explorer控制器文件夹中的控制器,所以这是我的问题,如何在控制器文件夹中反映控制器类< / p>
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路由正常工作>
window.location.href = '@Url.Action("common_dll", "Home")?ctrl=Test';
需要通过C#程序自动在Controller文件夹中反映该类,而无需手动进行。
答案 0 :(得分:0)
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.