我的问题有点类似于
How to get a response "stream" from an action in MVC3/Razor?

但我尝试了他们的方法,但没有成功。

细节

我正在使用MVC3,.net4,c#,

javascript和第3方组件来打开文件。

我有一个连接到viewStuff.jsViewFile.aspx

在我的viewStuff.js

var component = 'code to initialize'

先前

我曾经将aspx页连接到此javascript,它们工作得很好

在我的viewStuff.js

component.openFile("http://localhost:8080/ViewFile.aspx");

重定向到aspx页面

ViewFile.aspx.cs文件以HTTPResponse的形式返回与文件相关的数据

  protected void Page_Load(object sender, EventArgs e)
        {
            this.Response.Clear();

            string stuff = "abcd";
            this.Response.Write(stuff);

            this.Response.End();
        }


现在

我要做的就是用aspx替换该Controller,它将返回相同的内容。

在我的viewStuff.js

component.openFile("http://localhost:8080/ViewFile/Index");

Controller看起来像

public class ViewFileController: Controller{

   public ActionResult Index()
   {
     string stuff = "abcd";
     return stuff;
   }
}


我唯一的问题是我的component.openFile()方法无法使用MVC URL进入Controller
 一旦Index()开始,我就有了活动断点,但它们从未
 被击中。

我不知道它是否
   -网址
   -MVC-URL是方法而不是物理文件的事实

另外,我不确定如何弄乱Rou​​teConfig()是否有帮助。

编辑:路由配置:-

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );


(如果需要,我可以提供更多详细信息。在投票之前让我知道)

最佳答案

我想到2种可能性


从控制器操作中返回ContentResult

public class ViewFileController : Controller
{
   public ActionResult Index()
   {
       string stuff = "abcd";
       return Content(stuff);
   }
}

使用视图:

public class ViewFileController : Controller
{
   public ActionResult Index()
   {
       return View();
   }
}


并在相应的Index.cshtml视图中可以放置所需的任何标记。


同样,在控制器中放置任何断点之前,请在浏览器地址栏中打开http://localhost:8080/ViewFile/Index网址,并查看其是否返回正确的预期数据。

关于c# - 重定向到URL-ASP.NET与MVC,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19527126/

10-13 08:47