asp.net mvc 5 can i hide controller name from url

时间:2017-08-05 12:27:38

标签: asp.net asp.net-mvc-5

we have one project in asp.net mvc 5 when i run my project and click on some button for go to the next page then we are getting the url like wwww.abx.com/project/projectname .

where project is my controller name and projectname is Action name

so if i want so url like

wwww.abx.com/projectname

then this is possible ?

please if any one have some solution so please give it

here is my route.config file code

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "view1", id = UrlParameter.Optional }
            );
        }

Thanks

2 个答案:

答案 0 :(得分:0)

你必须创建一条新路线:

<?php
require "../ht.php"; $hom = new ht;
    if($_GET['vr']){
        $q = $_GET['vr'];

        if($q =='staff'){
            echo "
                <td align='right' style='width: 100%;'>Staff Name : </td>
                <td align='left' style='width: 100%;'>
                    <select name='vic_name' class='sect' style='width: 100%;'  value='<?php echo $edit[2] ?>' placeholder='Staff's Name'>";
                            $staf = mysql_query("SELECT * FROM  useraccounts"); $w=mysql_error();
                            while ($row = mysql_fetch_array($staf)) {
                                echo "<option value='$row[0]'>". $row[1] ."</option>"; 
                                # code...
                            } 
                    echo "</select>
                </td>
            ";
        }elseif ($q == 'student') {
            echo "
                <td align='right' style='width: 100%;'>Student Name: </td>
                <td align='left' style='width: 100%;'>
                    <select style='width: 100%;' class='sect' name='vic_name' value='".$edit[2] ."' placeholder='Student's Name' cols='9'>
                        <option></option>";
                        $stud= mysql_query("SELECT * FROM studentdetails");
                        while ($row = mysql_fetch_array($stud)) {
                            echo "<option value='$row[31]'>". $row[0] .' '. $row[1] ."</option>"; 
                            # code...
                        } 
                    echo "</select>
                </td>
            ";
        }else{
            echo "
                <td align='right' style='width: 100%;'>Visitor Name : </td>
                <td align='left' style='width: 100%;'><input style='width: 100%;' type='text' name='vic_name' value='".$edit[2] ."'placeholder='Visitor's Name'></td>
            ";
        }

    }
?>
<script type="text/javascript">(function($){
  var code = $('.html-container').html();
  $('.html-viewer').text(code);
})(jQuery);</script>

答案 1 :(得分:0)

如果你想只为一个控制器执行此操作 - add a custom route,如@Hamed Javaheri所建议,or use attribute mapping as described here

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Project",
                url: "projectname",
                defaults: new { controller = "project", action = "projectname", id = UrlParameter.Optional }

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

如果您希望此功能适用于多条/大多数路由,那么您可能需要更改默认映射,有些类似于此问题:

ASP.NET MVC Default URL View

ASP.NET MVC Routing with Default Controller

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{action}",
                defaults: new { controller = "project", id = "" }
            );
        }