M
M
MyOnAsSalat2017-11-05 19:01:08
ASP.NET
MyOnAsSalat, 2017-11-05 19:01:08

How to get url part after action?

Need to get the rest (*****) from the url:
host/content/img/*******
I'm trying to output to the view but it's empty.
startup class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc;

namespace SiteCore
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddRouting();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{name}");

                routes.MapRoute(
                    name: "img",
                    template: "{controller}/{action}/{name}");
            });
        }
    }
}

controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace SiteCore.Controllers
{
    public class ContentController : Controller
    {
        public IActionResult img(HttpContext context)
        {
            //var image = System.IO.File.OpenRead(@"D:\SiteCore\SiteCore\Content\img\blend_s\"+ s);
            //return File(image, "image/jpeg");
            var routeValues = context.GetRouteData().Values;
            var action = routeValues["name"].ToString();
            ViewBag.name = action;
            return View();
        }
    }
}

view code
<@{
    Layout = null;
}
<!doctype html>
<html>
<head>
    <title>Hello ASP.NET MVC Core</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width">
</head>
<body>
    <h1>@ViewBag.name</h1>

</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2017-11-06
@korenb_white

Above the action, put the attribute A in the parameters, take not HttpContext , but {name} segment:
public IActionResult img(string name) { ... }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question