How to properly encode links to external URL in MVC Razor

前端 未结 5 1802
Happy的楠姐
Happy的楠姐 2021-01-03 21:26

This view suppose to show a list of hyperlinks, each pointing to an external URL. The goal is for the user to click one of these links and have their browser open a new tab

相关标签:
5条回答
  • 2021-01-03 21:36

    You need to take into account your RouteConfiguration.

    routes.MapRoute( name: "Default", url: "{controller}/{action}"

    because you are specifying the action link as the entire link that you want to redirect. I would recommend that you use the @rossipedia answer because you can make tricky things like putting a span inside the link

    0 讨论(0)
  • 2021-01-03 21:42

    Here to display link that are clickable in index page

         <td>         
            @Html.ActionLink(item.FileName, "../Uploads/Catalogue/"+item.FileName)
        </td>
    
    0 讨论(0)
  • 2021-01-03 21:48

    While a ViewBag is overused and not the best choice most of the time this is something that I had done when inheriting someone else's mvc app to do a quick fix for a URL that I needed to redirect to with a specific dynamically changing querystring parameter

      <a target="_parent" href="http://localhost:56332/services/@ViewBag.factory">View Service</a>
    
    0 讨论(0)
  • 2021-01-03 21:54

    You don't need to use @Html.ActionLink for that. Just use a plain A tag:

    <a href="http://subdomain.mydomain.com/SomeSite">SomeSite</a>
    

    Html.ActionLink is specifically for generating links to actions defined in MVC controllers, in the same app. Since you're linking to an absolute URL, you don't need any of the functionality that Html.ActionLink provides.

    0 讨论(0)
  • 2021-01-03 22:00

    Two ways :

    1. update the database column with full link:

    eg SQL:

    update ProductTable set LinkColumn='http://www.example.com/Product/Mobiles' where ID=123
    

    In asp mvc view

    <a href="@model.ProductLink">View</a>
    

    2. Hardcode the http part and list from model

    <a href="http://@model.ProductLink">View</a>
    

    hope helps someone.

    0 讨论(0)
提交回复
热议问题