我的问题有点类似于 How to get a response "stream" from an action in MVC3/Razor?
但我已经尝试过他们的方法但没有成功。
详情
我正在使用 MVC3、.net4、c#,
用于打开文件的 javascript 和第 3 方组件。
我有一个连接到 ViewFile.aspx
的 viewStuff.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 访问 Controller
。
Index()
一开始我就有了活跃的断点,但它们从来没有
被击中。
不知道是不是
- 网址
- MVC - URL 是方法而不是物理文件这一事实
此外,如果有帮助的话,我不确定如何弄乱 RouteConfig()。
编辑:路由配置:-
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
(如果需要,我可以获得更多详细信息。在投票前让我知道)
最佳答案
想到了两种可能
从您的 Controller 操作返回一个
ContentResult
:public class ViewFileController : Controller { public ActionResult Index() { string stuff = "abcd"; return Content(stuff); } }
使用 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/