简体   繁体   English

C#/ ASP.NET ActionLink /功能/错误链接

[英]C#/ASP.NET ActionLink/Functions/Wrong Linking

Users upload a file which then gets reviewed by a doctor. 用户上传文件,然后由医生检查。 The doctor then uploads his file stating their remarks and then the user can read the file. 然后,医生上传他的文件,说明他们的评论,然后用户可以阅读该文件。 At least, they SHOULD be able to. 至少,他们应该能够。 When the doctor uploads a file a new directory is created inside the user's folder and the item is uploaded into that folder. 当医生上传文件时,将在用户文件夹中创建一个新目录,并将该项目上传到该文件夹​​中。

Now the bug. 现在的错误。 On the details page users see that there is a file link to be downloaded so they can read the doctor's notes. 在详细信息页面上,用户看到有一个文件链接可供下载,因此他们可以阅读医生的笔记。 But when they click the link, the link comes back a 404. We found that if we remove the file from the Review folder and put it in the main user directory the link now works. 但是,当他们单击链接时,该链接会返回404。我们发现,如果我们从Review文件夹中删除文件并将其放在主用户目录中,则该链接现在可以使用。 Obviously the link is not pointing to the correct place, but as I do not know .NET or C# this has proven to be quite an annoyance. 显然,该链接未指向正确的位置,但是正如我所不知道的.NET或C#一样,这已被证明很烦人。

Here is part of the original code from the Details.cshtml page: 这是来自Details.cshtml页面的原始代码的一部分:

<table>
      <tr>
           <th colspan="4" class="display-label">Uploaded Documents</th>

      </tr>
           <tr>
                <th>FileName</th>
                <th>Download Files</th>
           </tr>
           @foreach (var file in ((IEnumerable<MyCombinedQueryModel>)ViewBag.Files))
           {<tr>
                 <td>
                      @Html.ActionLink("Download", "Download", new { id = Model.PatientsId, name = file.FileName })
                 </td>
                 <td>
                      @file.FileName
                 </td>
            </tr>

           }<tr>
            <th colspan="4" class="display-label">Download reviewer suggestion document</th></tr>
      <tr>
           <th>FileName</th>
           <th>Download Files</th>
      </tr>
           @if(Model.reviewed)
           {
                foreach (var file in ((IEnumerable<MyCombinedQueryModel>) ViewBag.Path))
                {
                     <tr>
                          <td>
                               @Html.ActionLink("Download", "Download", new {id = Model.PatientsId, name = file.FileName})
                          </td>
                          <td>
                               @file.FileName
                          </td>
                     </tr>
                }
           }

 </table>

And the part from DoctorsController.cs 还有来自DoctorsController.cs的部分

 //Download files string names
     public ActionResult Download(int? id, string name)
          {


               string fileName = name;


               var uploads = (from u in _db.Patients
                             where u.PatientsId == id
                             select u.FilesUrl).FirstOrDefault();


               if (uploads != null)
               {

                    string folder = Path.GetFullPath(uploads);
                    //HttpContext.Response.AddHeader("content-dispostion", "attachment; filename=" + fileName);
                    return File(new FileStream(folder +"/" + fileName, FileMode.Open), "content-dispostion", fileName);
               }

               throw new ArgumentException("Invalid file name of file not exist");
          }

Now in our database we have two fields, FilesUrl and PathUrl. 现在,在我们的数据库中,我们有两个字段,FilesUrl和PathUrl。 PathUrl holds the URL for the Reviewed file in the Review folder. PathUrl在Review文件夹中保存Reviewed文件的URL。 I think what's going on, if I'm correct, is that they are both calling the same function (Download) which ONLY links to FileUrl. 我认为,如果我正确的话,那是他们两个都在调用仅链接到FileUrl的同一函数(下载)。 I tried changing the actionLink in the reviewed part to "DownloadR" in the details page and then copied and pasted the Download function (renamed it to DownloadR and changed it to look for PathUrl) but it still didn't work correctly. 我尝试在详细信息页面中将审阅部分中的actionLink更改为“ DownloadR”,然后复制并粘贴了Download函数(将其重命名为DownloadR并更改为查找PathUrl),但仍无法正常工作。

Is there anything anyone sees here that is a problem or any advice as to how to fix it? 是否有人在这里看到任何问题或有关如何解决的建议? Thank you in advance for your time! 预先感谢您的宝贵时间!

It looks like an issue with FilesUrl vs PathUrl. 似乎是FilesUrl与PathUrl的问题。 If the patient uploaded files and Dr uploaded files are in different directories this controller method will not work with both. 如果患者上传的文件和Dr上传的文件位于不同的目录中,则此控制器方法将无法同时使用。

I would add a third argument to the Download method that specified if the file to download was from the Dr or patient. 我要在Download方法中添加第三个参数,该参数指定要下载的文件是来自Dr还是患者。

public ActionResult Download(int? id, string name, bool reviewDirectory)
...
var uploads = (from u in _db.Patients
               where u.PatientsId == id
               select (reviewDirectory ? u.PathUrl : u.FilesUrl)).FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM