我的问题有点类似 How to get a response "stream" from an action in MVC3/Razor?
但我尝试过他们的方法但没有成功。
详情
我正在使用MVC3,.net4,c#,
javascript和第三方组件打开文件。
我有viewStuff.js
与ViewFile.aspx
相关联。
在viewStuff.js
我有
var component = 'code to initialize'
PREVIOUSLY
我曾经将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();
}
NOW
我想要做的就是将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()
启动后我就会有活动断点,但从不这样做
受到打击。
我不知道是否是它
- URL
- MVC - URL是方法而不是物理文件
此外,我不确定如果这可能有所帮助,如何弄乱RouteConfig()。
编辑:路由配置: -
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
(如果需要,我可以获得更多详细信息。在downvoting之前让我知道)
答案 0 :(得分: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
网址,看看它是否返回正确和预期的数据。