c# - 重定向到 URL - asp.net 与 MVC

标签 c# javascript asp.net asp.net-mvc asp.net-mvc-3

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

但我已经尝试过他们的方法但没有成功。

详情

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

用于打开文件的 javascript 和第 3 方组件。

我有一个连接到 ViewFile.aspxviewStuff.js

在我的 viewStuff.js 我有

var component = '要初始化的代码'

以前

我曾经将 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 访问 ControllerIndex() 一开始我就有了活跃的断点,但它们从来没有 被击中。

不知道是不是
- 网址
- MVC - URL 是方法而不是物理文件这一事实

此外,如果有帮助的话,我不确定如何弄乱 RouteConfig()。

编辑:路由配置:-

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

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

最佳答案

想到了两种可能

  1. 从您的 Controller 操作返回一个 ContentResult:

    public class ViewFileController : Controller
    {
       public ActionResult Index()
       {
           string stuff = "abcd";
           return Content(stuff);
       }
    }
    
  2. 使用 View :

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

    在相应的 Index.cshtml View 中,您可以放置​​任何您想要的标记。

此外,在您的 Controller 中放置任何断点之前,请在您的浏览器地址栏中打开 http://localhost:8080/ViewFile/Index url 并查看它是否返回正确和预期的数据。

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

相关文章:

asp.net - 使用 apache 和 mod_mono 调用堆栈缺少关于单声道的信息

c# - 鼠标悬停在日历控件上时的 Jquery 弹出窗口

c# - 我是否需要在我的对象中实现处置或终结?

c# - 这个似乎使用匿名函数和 javascript 样式属性设置的 C# 语法的目的是什么

c# - 使用 ajax 将 DropDownList 中的值传递给 Controller

javascript - 如何让文本已经在输入中并在聚焦时关闭?

c# - 保留回发时在客户端设置的表格单元格背景颜色

c# - 将数组的值保存在一个字符串中

javascript - 如何实现图像淡入淡出过渡?

javascript - Sails.js 如何在 async.series([ ]) 中使用 for 循环?