Как добавить класс контроллера в папку контроллера MVC с помощью программируемого - PullRequest
0 голосов
/ 17 мая 2019

Я создал Dynamic Controller в MVC и этот файл имел в папке Controller, но создал контроллер, не отображаемый в папке контроллера Solution-Explorer, вручную добавив, что динамический контроллер работает нормально, поэтому здесь моя проблема, как отразить класс контроллера в папке Controller

        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 Routing:

window.location.href = '@Url.Action("common_dll", "Home")?ctrl=Test';

должен автоматически отражать этот класс в папке Controller программой C # без использования вручную.

1 Ответ

0 голосов
/ 17 мая 2019
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.
/7922143/vyzov-ajax-v-kontroller-mvc-problema-s-url

 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. 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...