MVC3更改URL [英] MVC3 change the url
问题描述
我们有一个模块,生成并存储报告以PDF格式。
URL,以获取文档,例如像这样的:
https://domain.com/Document/GetDocument?documentId=00000000-0000-0000-0000-000000000000
此URL将返回文档名称为: Document_UserName_Date
不过,因为有浏览器就可以查看Chrome等PDF文件,该文件将在上述网址浏览器选项卡查看权限。</ P>
所以,当用户试图在文档保存在自己的电脑,默认的文件名(从网址获得)为: HTTPS ___ domain.com.pdf
而不是 Document_UserName_Date.pdf
如我们预期。
所以我想,如果我可以改变的网址为:
https://domain.com/Document/Document_UserName_Date.pdf
我的问题将得到解决。
为了避免文件被显示在浏览器,但用户直接提示下载它,你可以使用设置内容-Disposition
头附件
:
内容处置:附件;文件名=Document_UserName_Date.pdf
这可以通过简单地传递文件名作为第三个参数文件过载来完成:
公众的ActionResult GetDocument(GUID documentId)
{
字节[] =文件GetDocument(documentId);
返回File(文件,应用程序/ PDF,Document_UserName_Date.pdf);
}
更新:
如果您希望用户在查看他的浏览器文件内联,那么你可以使用路由和定义以下路线:
routes.MapRoute(
ViewPdfRoute
文件/ {ID} / {name}的.PDF
新{控制器=家,行动=GetDocument}
);
和在你的控制器动作:
公众的ActionResult GetDocument(GUID ID,字符串名称)
{
字节[] = PDF GetDocument(ID);
返回文件(PDF,应用程序/ PDF格式);
}
最后,你可以申请一个文件是这样的:
<$p$p><$c$c>http://domain.com/document/0DF7E254-0576-4BC0-8B05-34FC0F5246A2/document_username_date.pdfWe have a module to generate and store report in PDF format.
The url to get document for example like this:
https://domain.com/Document/GetDocument?documentId=00000000-0000-0000-0000-000000000000
This url will return a document with name: Document_UserName_Date
.
However, since there are browser can view PDF file like Chrome, the document will be view right in a tab of browser with above url.
So, when users try to save that document in their computers, the default file name(which get from url) is: https___domain.com.pdf
instead of Document_UserName_Date.pdf
as we expected.
So I'm thinking, if I can just changed the url into:
https://domain.com/Document/Document_UserName_Date.pdf
my problem will be solved.
In order to avoid the document being shown in the browser, but the user directly prompted to download it, you could use set the Content-Disposition
header to attachment
:
Content-Disposition: attachment; filename="Document_UserName_Date.pdf"
This could be done by simply passing the filename as third argument to the File overload:
public ActionResult GetDocument(Guid documentId)
{
byte[] document = GetDocument(documentId);
return File(document, "application/pdf", "Document_UserName_Date.pdf");
}
UPDATE:
If you want the user to view the document inline in his browser then you could use routing and define the following route:
routes.MapRoute(
"ViewPdfRoute",
"document/{id}/{name}.pdf",
new { controller = "Home", action = "GetDocument" }
);
and in your controller action:
public ActionResult GetDocument(Guid id, string name)
{
byte[] pdf = GetDocument(id);
return File(pdf, "application/pdf");
}
And finally you could request a document like this:
http://domain.com/document/0DF7E254-0576-4BC0-8B05-34FC0F5246A2/document_username_date.pdf
这篇关于MVC3更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!