我的问题有点类似于
How to get a response "stream" from an action in MVC3/Razor?
但我尝试了他们的方法,但没有成功。
细节
我正在使用MVC3,.net4,c#,
javascript和第3方组件来打开文件。
我有一个连接到viewStuff.js
的ViewFile.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是方法而不是物理文件的事实
另外,我不确定如何弄乱RouteConfig()是否有帮助。
编辑:路由配置:-
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/